Loading TOC...


   $old as Node,
   $new as Node
) as null


Replaces a node.

$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.


// assume /foo.json is {"foo":"some value"}  
var doc = cts.doc("/foo.json");
var docObj = doc.toObject();
docObj.foo = "this is a different value";
xdmp.nodeReplace(doc, docObj);
// now /foo.json will look like: {"foo":"this is a different value"}


// create an XML document

xdmp.documentInsert("/example.xml", fn.head(xdmp.unquote(

// replace the b node with a c node

var n = new NodeBuilder();
// create a <c>ccc</c> node
node = n.addElement("c", "ccc").toNode();
xdmp.nodeReplace(cts.doc("/example.xml").xpath("/a/b"), node);

// look at the new document

<?xml version="1.0" encoding="UTF-8"?>


// This example shows how to update the root
// node of a text format document.  Start by
//   creating a text document.

var n = new NodeBuilder();
node = n.addText("This is a line of text.").toNode();
xdmp.documentInsert("/mydir/doc.txt", node);


//  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.

var newText = fn.concat(cts.doc("/mydir/doc.txt"), "\n\
This is another line of text.");
var n = new NodeBuilder();
node = n.addText(newText).toNode();
xdmp.nodeReplace(cts.doc("/mydir/doc.txt").xpath("/text()"), node);


// look at the updated document
This is a line of text.
This is another line of text.


// create a document
xdmp.documentInsert("/foo.json", {"foo":"this is a value"});

// replace the value using xdmp.nodeReplace
var n = new NodeBuilder();
node = n.addText("this is a different value").toNode();
xdmp.nodeReplace(cts.doc("/foo.json").xpath("/foo"), node);

// show the new doc
{"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.


The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • I thought it is worth noting that developers don't need to use an in-memory node replace function as was the case when manipulating in memory XML docs with XQuery. David mentions it in this blog post. http://blog.davidcassel.net/2014/01/recursive-descent-in-xquery/ In-memory json is straight forward. var json = { "name": "Oliver", "scores": [88, 67, 73], "isActive": true, "affiliation": null } // change name using dot notation with string assignment. json.name = "Jane"; json;
    • Write a reusable script to update attributes or triples (Embedded triples) from a json document using Javascript in Marklogic, please help in this.
  • when there is a trail for update of node, it is saying that declareUpdate() operation is not allowed.
    • What do you mean by a trail? Can you show example code?
      • I tried doing this sample. xdmp:document-insert("/example.xml", < a >< b >bbb< / b >< /a >); xdmp:node-replace(doc("/example.xml")/a/b, <c>ccc</c>); when running this, I come to find that declareUpdate() operation is not allowed.
        • The "******" markers separate what are intended to be run as separate requests. Do the documentInsert(), then do the nodeReplace() in a separate request.
          • Ok but please, can you be some more clear.
            • Okay. I assume you're using Query Console. Using the first example, copy the declareUpdate() and documentInsert() commands to a buffer in Query Console. Click Run. That's one request (and one transaction). Now copy & paste the example code between the two "******" markers, from the "// replace the b node with a c node" comment through (and including) the xdmp.nodeReplace(...) command. Click Run. This second request updates the document inserted by the first request. Finally, copy & paste the last portion of the example code: the cts:doc(...); command. Click Run, and you'll see the updated document. The text shown in the example after the "=>" shows the result of running these commands.