I need to insert a new element into an XML document using XQuery insert expression. I am using saxon
as a java api. I am new to XQuery so I am not sure about the exact structure of the insert expression. Can anyone help me in this please.
My XML file looks like the following:
<?xml version="1.0" encoding="ISO-8859-1"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup<description>
<calories>650</calories>
</food>
</breakfast_menu>
and the java code I have for executing the insertion is
public void insert() throws XQException{
String queryString =
//"declare variable $docName as xs:string external;" + sep +
"variable $stores := doc("+"food.xml"+")/*;"+
"insert node element food {"+
"element name { \"brnj\" },"+
"element price { \"$20\" },"+
"element description { \"whatever\" },"+
"element calories { 300 },"+
"} into $stores;";
XQPreparedExpression expr = conn.prepareExpression(queryString);
expr.bindObject(new QName("stores"), "food.xml", null);
XQResultSequence rs = expr.executeQuery();
}
the error I get is in the syntax of the query string. Please help.
BaseX XQJ API supports XQuery Update Facility 1.0 and 3.0, which allows you to update documents in the BaseX database.
You can also update in-database documents via the MarkLogic XQJ API, Sedna XQJ API and eXist XQJ APIs although not via XQuery Update Facility, as these implementations do not support XQUF (yet).
Finally and possibly most importantly for your question, you may find Ryan Grimm's "in-mem-update" library module very useful [1]. I certainly have, countless times in the past.
You can
Also, just to make something clear here, the XQJ 1.0 Specification doesn't disallow vendors from supporting XQuery Update Facility.
An excerpt from the Specification:
"18.6 Support for Updates and Transactions
Although update functionality is not part of [XQuery], XQJ expects that some implementations will include some proprietary extensions to handle update functionality."
[1] https://github.com/marklogic/commons/blob/master/memupdate/in-mem-update.xqy