<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.nodeReplace.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.nodeReplace</api:function-name><api:suggest>xdmp.nodereplace</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>nodereplace</api:suggest><api:function-link mode="xquery" fullname="xdmp:node-replace">/apidoc/8.0/xdmp:node-replace.xml</api:function-link><api:function mode="javascript" name="nodeReplace" type="builtin" lib="xdmp" category="UpdateBuiltins" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.nodeReplace"><api:summary>
  Replaces a node.
</api:summary><api:params><api:param name="old" type="node()"><api:param-description>
    The old node, to be replaced.
  </api:param-description><api:param-name>old</api:param-name><api:param-type>Node</api:param-type></api:param><api:param name="new" type="node()"><api:param-description>
    The new node.
  </api:param-description><api:param-name>new</api:param-name><api:param-type>Node</api:param-type></api:param></api:params><api:return>null</api:return><api:usage>
  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.
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// 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"}
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// create an XML document

declareUpdate();
xdmp.documentInsert("/example.xml", xdmp.unquote(
  '&lt;a&gt;&lt;b&gt;bbb&lt;/b&gt;&lt;/a&gt;').next().value);

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

declareUpdate();
var n = new NodeBuilder();
// create a &lt;c&gt;ccc&lt;/c&gt; 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");
 =&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;a&gt;&lt;c&gt;ccc&lt;/c&gt;&lt;/a&gt;
  
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// 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")
=&gt;
This is a line of text.
This is another line of text.

</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// 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");
=&gt;
{"foo":"this is a different value"}
</pre></api:example></api:function></api:function-page>