Replacing Node Values


The replace expression is an updating expression that can be used to replace a node or a node’s value. Its syntax varies based on the type of replacement.

Example

The change-values.xq example queries holdings.xml. It looks for a user, Minollo (/table/holdings[UserId eq 'Minollo']), and increases his stock holdings (<shares>) by 20%, replacing the current value with the calculated value.

Results of the query are written to a new XML document, more-shares.xml, using the fn:put() function:

for $holding in doc("/examples/xml/holdings.xml") 
      /table/holdings[UserId eq 'Minollo'] 
return 
replace value of node $holding/Shares with xs:integer($holding/Shares * 1.2), 
       put(doc("/examples/xml/holdings.xml"), "/examples/xml/mo-shares.xml")