logo       

Re: XUpdate: how to replace a node?: msg#00221

text.xml.exist

Subject: Re: XUpdate: how to replace a node?

Hi,

> Hi, I need to replace a given node in my database by a whole new node.
> First, is the XUpdate documentation found on
> http://xmldb-org.sourceforge.net/xupdate/xupdate-wd.html current
> enough?
>
> Well, this documentation doesn't mention any replace functionality. So
> I thought that I could just remove the node, and add the new node to
> the parent, at the same position. Problem: in order to be able to
> append the new node to the parent, I need to put a reference to the
> parent in a variable before deleting the old node: my XPath to the
> node is like //node[@id="2"], so I can easily find the parent before
> deleting the node, but it is impossible afterwards.

I first tried the approach using variables. However, this didn't work,
basically because the declared variables were not visible from within the
select. I have thus fixed the code, so the following code snippet does the
job now (example taken from the corresponding junit test
"src/org/exist/xupdate/test/modifications/variables.xml"):

<xu:variable name="address" select="//address[lname = 'Hyde']"/>
<xu:variable name="root" select="$address/.."/>

<xu:remove select="$address"/>
<xu:append select="$root">
<address>
<fname>Boris</fname>
<lname>Becker</lname>
<company>BB-Inc.</company>
<city>Leimen</city>
</address>
</xu:append>

However, I also added an experimental xupdate:replace command. The code
differs only slightly from xupdate:update. Using xupdate:replace, you can
directly do:

<xu:replace select="//address[lname = 'Hyde']">
<address>
<fname>Boris</fname>
<lname>Becker</lname>
<company>BB-Inc.</company>
<city>Leimen</city>
</address>
</xu:replace>

which is definitely much simpler. You will find both changes in the current
CVS or snapshot.

> I also tried to define the variables in the surrounding xquery, and
> use them from within the xupdate, but I get an error saying "select
> expression should evaluate to a node-set; got xs:integer". This is the
> syntax I used:
> let $node := document('/db/doc.xml')//node[@n='2']
> let $parent := $node/..
>
> let $mods := xmldb:update($root,
> <xu:modifications version="1.0" xmlns:xu="http://www.xmldb.org/xupdate";>
> <xu:append select="{$parent}" child="0">
> <foo/>
> </xu:append>
>
> </xu:modifications>
>
> )

This can't work: the xupdate command uses its own instance of the XPath/XQuery
engine, so there's no way to pass variables from the XQuery to the XUpdate.
You would have to pass the XPath for the select as a string. That's why I
would like to implement the XQuery update extensions.

Wolfgang


-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM.
Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise