Loading TOC...

xdmp:node-replace

xdmp:node-replace(
   $old as node(),
   $new as node()
) as empty-sequence()

Summary

Replaces a node.

Parameters
$old The old node, to be replaced.
$new The new node.

Usage Notes

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.

Example

(: 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>

Example

(: 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.

Example

(: 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"}

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.

Comments

The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • I'd be careful not to include syntax that is not supposed to be executed within the example block with the same formatting as syntax that is supposed to be executed. You include => "goes to" and the example of the output document, but I had to really look and think to figure out if that was XQuery or not. Furthermore, I'm not sure the example is correct. Trying to update a simple test user document in my database fails: xdmp:node-replace(fn:doc('/user/usertest.xml')/enabled, <enabled>False</enabled>) XDMP-ARGTYPE: (err:XPTY0004) xdmp:node-replace((), <enabled>False</enabled>) -- arg1 is not of type node()
    • So the gotcha here is that the context returned by fn:doc is not changed to the document root element, so the correct syntax is: xdmp:node-replace(fn:doc('/user/usertest.xml')rootElementName/enabled, <enabled>False</enabled>)
      • Your example above (doing a node-replace on /element of your document) would work if element was the root element.of /user/usertest.xml. You are correct, fn:doc returns document nodes, not element nodes.