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.

If the caller of the function uses function mapping and $old is an empty sequence, the node-replace function may return an empty sequence. It will not return an error.

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"}
Powered by MarkLogic Server | Terms of Use | Privacy Policy