xdmp.nodeInsertChild

xdmp.nodeInsertChild(
   parent as Node,
   new as Node
) as null

Summary

Adds a new last child to a node. For XML documents, only element nodes and document nodes can have children. For JSON documents, object nodes and array nodes can have children. Element nodes, object nodes, and array 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.

Parameters
parent The parent node which will have a new child node.
new The new child node to be inserted.

Example

// create a document

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

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

declareUpdate();
xdmp.nodeInsertChild(cts.doc("/example.xml").xpath("/a"),
    fn.head(xdmp.unquote('<b>bbb</b>')).root);

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

cts.doc("/example.xml");
 =>
<?xml version="1.0" encoding="UTF-8"?>
<a><b>bbb</b></a>

Example

// create a document

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

******
// 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");
 =>
<?xml version="1.0" encoding="UTF-8"?>
<a b="bbb"/>
Powered by MarkLogic Server | Terms of Use | Privacy Policy