xdmp:node-replace( $old as node(), $new as node() ) as empty-sequence()
Replaces a node.
Parameters | |
---|---|
old | The old node, to be replaced. |
new | The new node. |
Attribute nodes cannot be replaced by non-attribute nodes. Non-attribute nodes cannot be replaced by attribute nodes. Element nodes cannot have document node children. Document nodes cannot have multiple roots.
If the caller of the function uses function mapping and $old
is an empty node, the node-replace
function may return an empty
sequence. It will not return an error.
(: create an XML document :) xdmp:document-insert("/example.xml", <a><b>bbb</b></a>); (: replace the b node with a c node :) xdmp:node-replace(doc("/example.xml")/a/b, <c>ccc</c>); (: look at the new document :) fn:doc("/example.xml") => <?xml version="1.0" encoding="UTF-8"?> <a><c>ccc</c></a>
(: This example shows how to update the root node of a text format document. Start by creating a text document. :) xdmp:document-insert("/mydir/doc.txt", text{"This is a line of text."} ) ; (: Update the text node of the text document by appending another line of text to the text node. Note that the text node is the root node of a text document. :) xdmp:node-replace(doc("/mydir/doc.txt")/text() , text{ concat(doc("/mydir/doc.txt")/text(), " This is another line of text.") } ) ; doc("/mydir/doc.txt") => This is a line of text. This is another line of text.
(: create a document :) xdmp:document-insert("/foo.json", object-node {"foo":"this is a value"}); (: replace the value using xdmp:node-replace :) xdmp:node-replace(fn:doc("/foo.json")/foo, text{"this is a different value"}); fn:doc("/foo.json") => {"foo":"this is a different value"}