Loading TOC...

xdmp.nodeReplace

xdmp.nodeReplace(
   $old as Node,
   $new as Node
) as null

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.

Example

// assume /foo.json is {"foo":"some value"}
declareUpdate();
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"}

Example

// create an XML document

declareUpdate();
xdmp.documentInsert("/example.xml", fn.head(xdmp.unquote(
  '<a><b>bbb</b></a>')));

******
// replace the b node with a c node

declareUpdate();
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

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

declareUpdate();
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.     

declareUpdate();
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
cts.doc("/mydir/doc.txt")
=>
This is a line of text.
This is another line of text.

Example

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

******
// replace the value using xdmp.nodeReplace
declareUpdate();
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
cts.doc("/foo.json");
=>
{"foo":"this is a different value"}

Comments

  • 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;
  • 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.
Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy