Skip to content

xml-object-reference

This section provides reference details for the properties and methods of the XML object itself, and for the related utility objects and global functions that you use to work with namespaces:

The XML object provides both static properties and functions, available through the XML class, and dynamic properties and functions available through each instance.

The constructor returns the XML object representing the root node of an XML tree, which contains additional XML objects for all contained elements.

[new] XML (xmlCode);

PropertyTypeDescription
xmlCodeString or XMLA string containing valid XML code, or an existing XML object.
- If a valid string is supplied, returns a new XML object encapsulating the XML code. If the XML code cannot be parsed, throws a JavaScript error.
- If an existing object is supplied and the new operator is used, returns a copy of the object; otherwise, returns the object itself.

These static properties are available through the XML class. They control how XML is parsed and generated:

PropertyTypeDescription
ignoreCommentsBooleanDescription When true, comments are stripped from the XML during parsing. Default is false.
ignoreProcessingInstructionsBooleanDescription When true, processing instructions (<?xxx?> elements) are stripped from the XML during parsing. Default is false.
ignoreWhitespaceBooleanDescription When true, white-space characters are stripped from the XML during parsing. Default is true.
prettyIndentNumberDescription The number of spaces to use for indenting when pretty-printing. Default is 2.
prettyPrintingBooleanDescription When true, toXMLString() uses indenting and line feeds to create the XML string. Default is true.

These static functions are available through the XML class, and provide information about the global settings of the XML parser.

XML.defaultSettings();

Retrieves the default global option settings that control how XML is parsed and generated.

Returns a JavaScript object containing five properties, which correspond to the five XML Settings.


XML.settings();

Retrieves the current global option settings that control how XML is parsed and generated.

Returns a JavaScript object containing five properties, which correspond to the five XML Settings


XML.setSettings(object);

Sets the global option settings that control how XML is parsed and generated. You can use this to restore settings retrieved with settings() or defaultSettings().

ParameterTypeDescription
objectObjectA JavaScript object containing five properties, which correspond to the five XML Settings

Nothing


The properties of the XML object are named for and contain the values of the child elements and attributes of the element that the object represents.

xmlObj.childElementName

Child-element properties are named with the child element name.

XML object


xmlObj.@attributeName

Attribute properties are named with the attribute name prefixed with the at-sign, @.

XML object


xmlObj.addNamespace(ns);

Adds a namespace declaration to this node.

ParameterTypeDescription
nsNamespace objectNamespace declaration to add

This XML object.


xmlObj.appendChild(child);

Appends a child element to this node, after any existing children. If the argument is not XML, creates a new XML element that contains the string as its text value, using the same element name as the last element currently contained in this object’s node.

ParameterTypeDescription
childXML object, or any value that can be converted to a String with toString()Child element to append

This XML object.


xmlObj.attributes(name);

Retrieves a list of the named attribute elements contained in this node.

ParameterTypeDescription
nameStringAhe attribute name.

An XML object containing all values of the named attribute.


xmlObj.child(which);

Retrieves a list of all child elements of this node of a given type.

ParameterTypeDescription
whichString or NumberThe element name, or a Number, a 0-based index into this node’s child array.

An XML object containing all child elements of the given type.


xmlObj.childIndex ();

Retrieves the 0-based position index of this node within its parent node.

Number


xmlObj.children();

Retrieves all of the immediate child elements of this node, including text elements.

An XML object containing the child elements.


xmlObj.comments();

Retrieves all XML comment elements from this node.

An XML object containing the comments.


xmlObj.contains(element);

Reports whether an element is contained in this node at any level of nesting.

ParameterTypeDescription
elementXML objectElement to check

Boolean. true if the element is contained in this XML tree.


xmlObj.copy();

Creates a copy of this node.

The new XML object.


xmlObj.descendants([name]);

Retrieves all descendent elements of this node of a given element type, or all XML-valued descendants, at any level of nesting. Includes text elements.

ParameterTypeDescription
nameStringOptional. The element name to match. If not provided, matches all elements.

An XML object containing properties for each descendant element.


xmlObj.elements(name);

Retrieves all of the immediate child elements of this node of the given type, or of all types. Does not include text elements.

ParameterTypeDescription
nameStringOptional. The element name to match. If not provided, matches all elements.

An XML object containing properties for each child element.


xmlObj.hasComplexContent();

Reports whether this node has complex content; that is, whether it contains child elements. Disregards contents of other kinds, including attributes, comments, processing instructions and text nodes.

Boolean. true if this node contains child elements.


xmlObj.hasSimpleContent();

Reports whether this node has simple content; that is, whether it represents a text node, an attribute node, or an element without child elements (regardless of whether it also contains attributes, comments, processing instructions or text).

Object representing comments and processing instructions do not have simple content.

Boolean. true if this node contains no child elements.


xmlObj.inScopeNamespaces();

Retrieves the current list of valid namespaces in this element.

An Array of Namespace object, in which the last member is the default namespace.


xmlObj.insertChildAfter(child1, child2);

Inserts a new child element or text node into this node, after another existing child element. If the relative element is not currently in this node, does not insert the new child.

ParameterTypeDescription
child1XML objectThe existing child element after which to place the new child, or null to insert the new child at the beginning.
child2XML objectThe new child element, or any value that can be converted to a String with toString().

This XML object.


xmlObj.insertChildBefore(child1, child2);

Inserts a new child element or text node into this node, before another existing child element. If the relative element is not currently in this node, does not insert the new child.

ParameterTypeDescription
child1XML object The existing child element before which to place the new child, or null to insert the new child at the end.
child2XML object The new child element, or any value that can be converted to a String with toString().

This XML object.


xmlObj.length();

Reports the number of child elements contained in this node. The minimum number is 1, the element that this object represents.

Number


xmlObj.localName();

Retrieves the local name of this element; that is, the element name, without any namespace prefix.

String


xmlObj.name();

Retrieves the full name of this element, with the namespace information.

A QName object containing the element name and namespace URI.


xmlObj.namespace();

Retrieves the namespace URI of this element.

String


xmlObj.nodeKind();

Reports the type of this node.

A String, one of:

  • element
  • attribute
  • comment
  • processing-instruction
  • text

xmlObj.namespaceDeclarations();

Retrieves all of the namespace declarations contained in this node.

An Array of Namespace object.


xmlObj.normalize();

Puts all text nodes in this and all descendant XML objects into a normal form by merging adjacent text nodes and eliminating empty text nodes.

This XML object.


xmlObj.parent();

Retrieves the parent node of this node.

An XML object, or null for the root element.


xmlObj.prependChild(child);

Prepends a child element to this node, before any existing children. If you prepend a string to a text element, the result is two text elements; call normalize() to concatenate them into a single text string.

ParameterTypeDescription
childXML object or StringChild element to prepend

This XML object.


xmlObj.processingInstructions ([name]);

A String, the name of a processing instruction, or null to get all processing instructions.

Retrieves processing instructions contained in this node.

An XML object containing the children of this object that are processing instructions, matching the name if supplied.


xmlObj.replace(name, value);

Replaces one or more property values in this node.

If the named element does not exist, appends the given value as a text element.

ParameterTypeDescription
nameStringAn element or attribute name, with or without the 0-based position index of a specific element, or the wildcard string "*".
- If no position index is supplied, replaces the value of all matching elements.
- If the wildcard is supplied, replaces the value of all contained elements. When an element contain subelements, those are removed, and only the replacement value remains.
valueXML object or any value that can be converted to a String with toString()Value to replace with

This XML object.


xmlObj.setChildren(value);

Replaces all of the XML-valued properties in this object with a new value, which can be a simple text element, or can contain another set of XML properties.

ParameterTypeDescription
valueXML object or any value that can be converted to a String with toString().Value to replace with

This XML object.


xmlObj.setLocalName(name);

Replaces the local name of this object; that is, the element name without any namespace prefix.

ParameterTypeDescription
nameStringThe new name.

This XML object.


xmlObj.setName(name);

Replaces the full name of this object; that is, the element name and its namespace prefix.

ParameterTypeDescription
nameStringThe new name.

This XML object.


xmlObj.setNamespace(ns);

Sets the namespace for this XML element. If the namespace has not been declared in the tree above this element, add a namespace declaration instead.

ParameterTypeDescription
nsNamespace objectNamespace that has been declared in the tree above this element.

This XML object.


xmlObj.text();

Retrieves text nodes from this element.

An XML object containing all properties of this object that represent XML text nodes.


xmlObj.toString();

Creates a string representation of this object.

  • For text and attribute nodes, this is the textual value of the node.
  • For other elements, it is the result of toXMLString().
  • If this XML object is a list, concatenates the result of calling the function on each contained element.

String


xmlObj.toXMLString();

Creates an XML-encoded string representation of this XML object.

This result includes the start tag, attributes and end tag of the XML object, regardless of its content. Formats the string as specified by the global settings XML.prettyPrinting and XML.prettyIndent.

String


xmlObj.xpath(expression[, variables]);

Evaluates an XPath expression in accordance with the W3C XPath recommendation, using this XML object as the context node. The context position and size are set to 1, and all variables are initially unbound. If this XML object is a list, evaluates all contained XML element nodes (not comments or other node types) and return the results in a list in the order of execution.

If the XPath expression does not evaluate to a node list, throws a JavaScript exception.

ParameterTypeDescription
expressionStringA String containing an XPath expression.
!!! note
In this context, include the actual top level element. For example, an expression for the example XML must start with “/bookstore”. This is unlike JavaScript property access, where the top level element is implied.
variablesObjectOptional. A JavaScript object containing variable definitions. The properties are used to look up XPath variables contained in the expression. For example, if the expression contains the variable $abc, the value is in the object’s abc property.

An XML object, the result of evaluation.


These functions are available in the JavaScript global namespace.

isXMLName(String name)

Reports whether a string contains a name that conforms to valid XML syntax.

ParameterTypeDescription
nameStringWhether the string is an XML Name

Boolean. true if the name is a valid XML name, false otherwise.


setDefaultXMLNamespace(Namespace ns)

Sets the default namespace for XML objects. You can also set the default namespace using this syntax:

default xml namespace = Namespace object
default xml namespace = URL_string
ParameterTypeDescription
nsNamespace objectObject to set as default. Any prefix is ignored.

Nothing


This object encapsulates a fully qualified XML name, the combination of a local XML name and its namespace URI.

The constructor takes several forms:

new QName ()
new QName (name)
new QName (ns)
new QName (uri, name)

When no arguments are supplies, creates a QName object with an empty local name and no URI.

ParameterTypeDescription
nameStringCreates a QName object with the given local name and the URI of the default namespace. Can be the wildcard character, ”*“.
nameQNameCreates a copy of an existing QName object.
nsNamespaceCreates a QName object with an empty local name and the URI of the Namespace object.
uri, nameStringCreate a QName object with the given namespace URI and local name. If the local name is supplied as the wildcard character, ”*”, the uri argument is ignored, and the URI value is that of the default namespace.

QName.name

The local element name portion of the XML element’s fully qualified XML name.

String


QName.uri

The namespace prefix of the XML element’s fully qualified XML name.

String


This object encapsulates the definition of an XML namespace. A namespace associates an XML-name prefix with a complete URI. The prefix is a string that precedes the local name of an XML element or attribute and identifies the namespace, while the URI points to the actual location where the definition of the namespace is found.

For example, this XML definition contains a namespace declaration:

<?xml xmlns:adobe=http://www.adobe.com/test?>

In the corresponding namespace, the prefix is adobe, and the URI is http://www.adobe.com/test.


The Namespace constructor takes several forms:

new Namespace()
new Namespace (String uri)
new Namespace (QName prefix)
new Namespace (Namespace ns)
new Namespace (String prefix, String uri)

When no argument is supplied, creates a namespace with an empty prefix and URI.

ParameterTypeDescription
uriStringCreates a Namespace object with an empty prefix and the given URI.
prefixQNameCreates a namespace with an empty prefix and the URI set to the URI of the QName object (if the QName object contains a URI).
nsNamespaceCreates a copy of the given Namespace object. If the Namespace() function is called without the new operator, and the only argument is a Namespace object, the function simply returns that object, rather than creating a copy.
prefix, uriStringCreates a Namespace object with the given prefix and the given URI.

namespace.prefix

The element-name prefix associated with the namespace URI. The prefix value can be undefined, as when a specified prefix is not a valid XML name.

Namespaces with an undefined prefix are completely ignored; they are not added to an XML namespace declaration.

String


namespace.uri

The location of the namespace definition, a URI.

String