pp108 : selectXMLNodes()

selectXMLNodes()


It searches for XML nodes that match the specified XPath expression and returns an object containing a collection of the nodes.

Syntax

Script cordys.selectXMLNodes(XMLNode, xpathExpression, [namespaces])


Parameters

Parameter Description
XMLNode Refer to the xmlNode or xmlDocument to search in.
xpathExpression Refers to the XPath expression that is set as the search criteria.
namespaces Optional. Refers to the namespace that is set in the search criteria. The value that you provide for this parameter must be an array of prefixes and namespaces; for example,{ "ns1": "namespace1", "ns2": "namespace2" }.


Return Value


Returns a collection of current nodes that match the specifiedxpathExpression.
The following properties are available for the list of nodes that is returned.

  • length: Read-only property. It indicates the number of items in the collection.
  • context: Refers to the context of the returned object that contains the xmlNode on xpathExpression.
  • expr: Refers to the string value that indicates thexpathExpressionused as the search criteria.
    The following methods are available for the list of nodes that is returned.
  • item(index): Enables random access to individual nodes in the collection. Theindexparameter refers to the index of an item in the collection and accepts an integer value. The index of the first item is zero. This method returns null value if the index is out of range.
  • nextNode(): Returns the next node in the collection. This method returns null value if there is no next node.
  • reset(): Reinitializes the iterator to point before the first node in the returned collection of nodes. This ensures that if the nextNode() method is used, it returns the first node in the list.

Remarks


XPath expressions can include namespaces. Namespace resolution is supported using thecordys.setXMLNamespaces()method or using the specified namespace argument of selectXMLNodes. If selectXMLNodes is called inside a loop, it helps improve performance if the namespaces are first set ascordys.setXMLNamespacesand then used in the loop. Providing namespaces for selectXMLNodes in each loop affects the performance.

If the XPath expression does not include a prefix, the empty namespace is considered as the namespace URI. If your XML includes a default namespace, ensure that you add a prefix and namespace URI, or no nodes will be selected.

Example
cordys.setXMLNamespaces(xmlNode, { "ns1":"namespace1" }  );
var nodeList = cordys.selectXMLNodes(xmlNode, ".//ns1:myxpath")
for ( var i=0, length=nodeList.length; i < length;  i++ )
{
	var currentNode = nodeList[i];
	var previousNode = nodeList.item(i-1);
	var nextNode = currentNode.nextNode();
}

Applies to


cordys