Q: Is there a way to update a XML node with subelements
Thursday, 29 March 2018 · 2 messages · last reply 30 Mar 2018
HF
Harald Freudenberger29 Mar 2018 16:37 #
Hello BaseX team
I am about to implement a simple web application which displays some items and allows an user to edit one item. So my database is simple:
data.xml:
<items>
<item>
<id>1</id>
<text>just some text</text>
</item>
<item>
<id>2</id>
<text>some other text</text>
</item>
</items>Now I want to add a new item to this database. For first evaluation I'll use curl to insert a new node with:
curl -u admin:admin -i -X PUT -H "Content-Type: application/xml" -d "<item><id>9999</id><text>hello</text></item>" "http://localhost:8984/rest/data/items"The query to check if the new node has been inserted gives:
curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns='http://basex.org/rest'><text>//item</text></query>" "http://localhost:8984/rest/data" HTTP/1.1 200 OK
Content-Type: application/xml; charset=UTF-8
Content-Length: 1416
Server: Jetty(8.1.18.v20150929)
<item>
<id>1</id>
<text>just some text</text>
</item>
<item>
<id>2</id>
<text>some other text</text>
</item>
<item>
<id>9999</id>
<text>hello</text>
</item>Fine. And now I'd like to try the edit an item.
I start with fetching the item:
curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns='http://basex.org/rest'><text>//item[id='9999']</text></query>" "http://10.0.2.2:8984/rest/data" HTTP/1.1 200 OK
Content-Type: application/xml; charset=UTF-8
Content-Length: 280
Server: Jetty(8.1.18.v20150929)
<item>
<id>9999</id>
<text>hello</text>
</item>Now I do some modifications:
<item>
<id>9999</id>
<text>new text here</text>
<newnode>a new node</newnode>
</item>And then I want to write back this item with:
curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns='http://basex.org/rest'><text>replace node /data/items/item[id='9999'] with <item><id>8888</id></item></text></query>" "http://10.0.2.2:8984/rest/crypto" HTTP/1.1 400 Bad Request
Content-Type: text/plain;charset=UTF-8
Content-Length: 104
Server: Jetty(8.1.18.v20150929)
cvc-type.3.1.2: Element 'text' is a simple type, so it must have no element information item [children].
I tried <command> instead of <query> but the result is always the same. Am I doing something wrong here ? Is there a way to update a node with subelements in BaseX ? Please understand, I am C developer dealing with Linux kernel code most of the time so I am not really familiar with all the web application things.
Thanks and regards
Harald Freudenberger
AH
Alexander Holupirek30 Mar 2018 21:43 #
Quoted text · Harald Freudenberger · 92 lines
> On 29. Mar 2018, at 16:37, Harald Freudenberger <freude at linux.vnet.ibm.com> wrote: > > Hello BaseX team > > I am about to implement a simple web application which displays some items and > allows an user to edit one item. So my database is simple: > > data.xml: > <items> > <item> > <id>1</id> > <text>just some text</text> > </item> > <item> > <id>2</id> > <text>some other text</text> > </item> > </items> > > Now I want to add a new item to this database. For first evaluation I'll use > curl to insert a new node with: > > curl -u admin:admin -i -X PUT -H "Content-Type: application/xml" -d "<item><id>9999</id><text>hello</text></item>" > "http://localhost:8984/rest/data/items" > > > The query to check if the new node has been inserted gives: > > curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns=' > http://basex.org/rest'><text>//item</text></query>" "http://localhost:8984/rest/data" > > HTTP/1.1 200 OK > Content-Type: application/xml; charset=UTF-8 > Content-Length: 1416 > Server: Jetty(8.1.18.v20150929) > > <item> > <id>1</id> > <text>just some text</text> > </item> > <item> > <id>2</id> > <text>some other text</text> > </item> > <item> > <id>9999</id> > <text>hello</text> > </item> > > Fine. And now I'd like to try the edit an item. > I start with fetching the item: > curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns=' > http://basex.org/rest'><text>//item[id='9999']</text></query>" "http://10.0.2.2:8984/rest/data" > > HTTP/1.1 200 OK > Content-Type: application/xml; charset=UTF-8 > Content-Length: 280 > Server: Jetty(8.1.18.v20150929) > > <item> > <id>9999</id> > <text>hello</text> > </item> > > Now I do some modifications: > > <item> > <id>9999</id> > <text>new text here</text> > <newnode>a new node</newnode> > </item> > > And then I want to write back this item with: > > curl -u admin:admin -i -X POST -H "Content-Type: application/xml" -d "<query xmlns=' > http://basex.org/rest'><text>replace node /data/items/item[id='9999'] with <item><id>8888</id></item></text></query>" "http://10.0.2.2:8984/rest/crypto" > > HTTP/1.1 400 Bad Request > Content-Type: text/plain;charset=UTF-8 > Content-Length: 104 > Server: Jetty(8.1.18.v20150929) > > cvc-type.3.1.2: Element 'text' is a simple type, so it must have no element information item [children]. > > I tried <command> instead of <query> but the result is always the same. > Am I doing something wrong here ? Is there a way to update a node with > subelements in BaseX ? Please understand, I am C developer dealing with > Linux kernel code most of the time so I am not really familiar with all > the web application things. > > Thanks and regards > Harald Freudenberger
Hi Harald,
you probably have to use CDATA for the parser and single quotes(') for the shell.
$ cat file.xml
<xml>
<a>
<b/>
</a>
<c/>
</xml>
$ curl -u admin:admin -X PUT -T 'file.xml' 'http://localhost:8984/rest/demo'
Database 'demo' created in 11.45 ms.
$ curl -u admin:admin "http://localhost:8984/rest/demo" -d '<query xmlns="http://basex.org/rest"><text> /xml </text></query>'
<xml>
<a>
<b/>
</a>
<c/>
</xml>
$ curl -i -u admin:admin "http://localhost:8984/rest/demo" -d '<query xmlns="http://basex.org/rest"><text><![CDATA[ replace node /xml/a/b with <foo/> ]]></text></query>'
HTTP/1.1 200 OK
Date: Fri, 30 Mar 2018 19:40:49 GMT
Content-Type: application/xml; charset=UTF-8
Content-Length: 0
Server: Jetty(9.4.8.v20171121)
$ curl -u admin:admin "http://localhost:8984/rest/demo" -d '<query xmlns="http://basex.org/rest"><text> /xml </text></query>'
<xml>
<a>
<foo/>
</a>
<c/>
</xml>
$ curl -i -u admin:admin "http://localhost:8984/rest/demo" -d '<query xmlns="http://basex.org/rest"><text>replace node /xml/c with 123</text></query>'
HTTP/1.1 200 OK
Date: Fri, 30 Mar 2018 19:42:02 GMT
Content-Type: application/xml; charset=UTF-8
Content-Length: 0
Server: Jetty(9.4.8.v20171121)
$ curl -u admin:admin "http://localhost:8984/rest/demo" -d '<query xmlns="http://basex.org/rest"><text> /xml </text></query>'
<xml>
<a>
<foo/>
</a>123</xml>Hope this helps,
Alex
Written for BaseX 9.0. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery