NodeBuilder.addElement( name as String, [content as String|Function], [namespace as String?] ) as This NodeBuilder object
Add an XML element node to the node tree the builder is building.
Element nodes may only be added to element nodes or document nodes.
To add a namespaced element, give a prefixed name as the first argument and a namespace URI as the final argument.
const builder = new NodeBuilder(); builder.addElement('ex:example', 'element content here', 'http://example.com/ns1'); builder.toNode(); // Returns an XML element node equivalent to the following: // <ex:example xmlns:ex="http://example.com/ns1">element content here</ex:example>
function addCommonAttrs(b) { b.addAttribute('class','body'); b.addAttribute('xml:lang','en','http://www.w3.org/XML/1998/namespace'); b.addAttribute('id', 'id_' + xdmp.random() ); }; const builder = new NodeBuilder(); builder.startElement('root'); builder.addElement('child', addCommonAttrs); builder.addElement('child', addCommonAttrs); builder.endElement(); builder.toNode(); // Returns the following XML (whitespace added for readability): // <root> // <child class="body" xml:lang="en" id="id_14938401739084225878" // xmlns:xml="http://www.w3.org/XML/1998/namespace"/> // <child class="body" xml:lang="en" id="id_602182576263533813" // xmlns:xml="http://www.w3.org/XML/1998/namespace"/> // </root>