Loading TOC...

NodeBuilder.addAttribute

NodeBuilder.addAttribute(
   name as String,
   text as String,
   [namespace as String?]
) as This NodeBuilder object

Summary

Add an attribute node to the node tree the builder is building.

Parameters
name The attribute name. This can be either a simple name, if the attribute is in no namespace, or a QName.
text The value of the attribute.
namespace The namespace URI associated with the prefix in the name.

Usage Notes

Attribute nodes may only be added to element nodes that have had nothing but other attribute nodes added to them.

To add a namespaced attribute, give a prefixed name as the first argument and a namespace URI as the final argument.

Example

const builder = new NodeBuilder();
builder.startElement('example');
  builder.addAttribute('xml:lang','en','http://www.w3.org/XML/1998/namespace');
  builder.addText('English content');
builder.endElement();
builder.toNode();

// Returns an XML element node equivalent to the following:
// <example xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">
//   English content
// </example>

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.