Loading TOC...

NodeBuilder.addElement

NodeBuilder.addElement(
   $name as String,
   [$content as String|Function],
   [$namespace as String?]
) as This NodeBuilder object

Summary

Add an XML element node to the node tree the builder is building.

Parameters
$name The name of the element. This can be either a simple name, if the element has no namespace, or a QName.
$content The contents of the element. If the argument is a string, the element will content that string as a text node. If the argument is a function, the function will be evaluated with the builder as its argument.
$namespace The namespace URI associated with the prefix in the name.

Usage Notes

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.

Example

const builder = new NodeBuilder();
builder.addElement("ex:example", "element content here",
       "http://example.com/ns1");
builder.toNode();

// Returns:
// <ex:example xmlns:ex="http://example.com/ns1">element content here</ex:example>

Example

function addCommonAttrs(b) {
  b.addAttribute("class","body");
  b.addAttribute("xml:lang","en","http://www.w3.org/XML/1998/namespace");
  b.addAttribute("id", fn.concat("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>

Comments

    Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy