<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.nodeInsertChild.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.nodeInsertChild</api:function-name><api:suggest>xdmp.nodeinsertchild</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>nodeinsertchild</api:suggest><api:function-link mode="xquery" fullname="xdmp:node-insert-child">/apidoc/8.0/xdmp:node-insert-child.xml</api:function-link><api:function mode="javascript" name="nodeInsertChild" type="builtin" lib="xdmp" category="UpdateBuiltins" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.nodeInsertChild"><api:summary>
  Adds a new last child to a node.
  Only element nodes and document nodes can have children.
  Element nodes cannot have document node children.
  Document nodes cannot have multiple roots.
  On-the-fly constructed nodes cannot be updated.
  The parameters must specify individual nodes and not node sets.
</api:summary><api:params><api:param name="parent" type="node()"><api:param-description>
    The parent node which will have a new child node.
  </api:param-description><api:param-name>parent</api:param-name><api:param-type>Node</api:param-type></api:param><api:param name="new" type="node()"><api:param-description>
    The new child node to be inserted.
  </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:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// create a document 

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

******
// insert a child of a 

declareUpdate();
xdmp.nodeInsertChild(cts.doc("/example.xml").xpath("/a"),
    xdmp.unquote('&lt;b&gt;bbb&lt;/b&gt;').next().value.root);

******
// look at the new document 

cts.doc("/example.xml");
 =&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;a&gt;&lt;b&gt;bbb&lt;/b&gt;&lt;/a&gt;
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// create a document 

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

******
// insert an attribute as child of a 

declareUpdate();
var n = new NodeBuilder();
node = n.addAttribute("b", "bbb").toNode();
xdmp.nodeInsertChild(cts.doc("/example.xml").xpath("/a"), node);

// look at the new document 

cts.doc("/example.xml");
 =&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;a b="bbb"/&gt;
</pre></api:example></api:function></api:function-page>