Question for XQJ to update
Thursday, 8 April 2010 · 2 messages
QX
Qu Xia8 Apr 2010 10:33 #
I made a simple XML called order.xml like this:
-------------order.xml--------------
<orders>
<order>
<name>cola</name>
<price>3.0</price>
<factory>Number 1</factory>
</order>
<order>
<name>coffee</name>
<price>5.0</price>
<factory>Number 2</factory>
</order>
</orders>
--------------------------------------------Now I want to update the price of the coffee to 6.0 with XQJ, so I got the following code:
String driverName = "org.basex.api.xqj.BXQDataSource";
Class<?> driverClass = Class.forName(driverName);
XQDataSource dataSource = (XQDataSource)driverClass.newInstance();
XQConnection xqc = dataSource.getConnection();
String updateString = "declare variable $node :=
doc('etc/xml/orders.xml')//order[name='coffee']; "+
"replace value of node $node/price with '5.0', " +
"return " +
"put(doc('etc/xml/orders.xml'), 'orders.xml')";
XQExpression xqu = xqc.createExpression();
xqu.executeQuery(updateString);
xqu.close();unfortunately, it throws an exception. I tried many different ways and fails all the time. could any tell me the right "updateString"?
thanks a lot.
--
Xia Qu
Technology Center of Software Engineering,
Institute of Software,
Chinese Academy of Sciences
Beijing, 100190, China
Email (work):quxia08 at otcaix.iscas.ac.cn
Email (personal): tsuxia at gmail.com
Mobile:(86) 1300-1010-069
AW
Andreas Weiler8 Apr 2010 11:04 #
Dear Qu Xia,
i just checked your query and the problem was at the put function. I have rewritten it like this:
put (copy $doc := doc('orders.xml') modify replace value of node
$doc//order[name='coffee']/price with '5.0' return $doc, 'orders.xml')Maybe there are other solutions but this worked for me.
Kind regards,
Andreas
Quoted text · Qu Xia · 56 lines
Qu Xia schrieb: > I made a simple XML called order.xml like this: > -------------order.xml-------------- > <orders> > <order> > <name>cola</name> > <price>3.0</price> > <factory>Number 1</factory> > </order> > <order> > <name>coffee</name> > <price>5.0</price> > <factory>Number 2</factory> > </order> > </orders> > -------------------------------------------- > > Now I want to update the price of the coffee to 6.0 with XQJ, so I got > the following code: > > String driverName = "org.basex.api.xqj.BXQDataSource"; > Class<?> driverClass = Class.forName(driverName); > XQDataSource dataSource = (XQDataSource)driverClass.newInstance(); > XQConnection xqc = dataSource.getConnection(); > > String updateString = "declare variable $node := > doc('etc/xml/orders.xml')//order[name='coffee']; "+ > "replace value of node $node/price with '5.0', " + > "return " + > "put(doc('etc/xml/orders.xml'), 'orders.xml')"; > XQExpression xqu = xqc.createExpression(); > xqu.executeQuery(updateString); > xqu.close(); > > unfortunately, it throws an exception. I tried many different ways and > fails all the time. could any tell me the right "updateString"? > > thanks a lot. > > -- > Xia Qu > Technology Center of Software Engineering, > Institute of Software, > Chinese Academy of Sciences > Beijing, 100190, China > Email (work):quxia08 at otcaix.iscas.ac.cn > <mailto:quxia08 at otcaix.iscas.ac.cn> > Email (personal): tsuxia at gmail.com <mailto:tsuxia at gmail.com> > Mobile:(86) 1300-1010-069 > ------------------------------------------------------------------------ > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
Written for BaseX 6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery