SelectNodes (XmlDoc/XmlNode function)

From m204wiki
Revision as of 09:41, 22 May 2011 by Goff (talk | contribs) (edits, tags and links)
Jump to navigation Jump to search

Get list of selected nodes as XmlNodelist (XmlDoc and XmlNode classes)

SelectNodes stores, in an XmlNodelist, the list of nodes selected by an Xpath expression.

Syntax

%nodl = nr:SelectNodes[( [xpath])] Throws XPathError

Syntax terms

%nodl An XmlNodelist that is set to point to the nodelist that is created.
nr An XmlDoc or XmlNode, used as the context node for the xpath expression. If an XmlDoc, the Root node is the context node.
xpath A Unicode string that is an Xpath expression that results in a nodelist. The method returns these nodes in an XmlNodelist. This is an optional argument whose default is a period (.), that is, the node referenced by the method object (nr).

Prior to "Sirius Mods" Version 7.6, this is an EBCDIC string.

Usage notes

  • If the result of the argument XPath expression (XPath) is empty, a Null is returned.
  • The nodes found by SelectNodes are arranged in the resulting nodelist in document order. For some sets selected by XPath expressions (for example, the set of ancestor nodes), document order may not be the ordering you intuitively expect. For a discussion of this issue, see “Order of nodes: node sets versus nodelists”.

Examples

  1. In the following example, SelectNodes creates an XmlNodelist, from which an element is selected to insert an element before:

    begin %doc is object xmlDoc %nl1 is object xmlNodelist %n1 is object xmlNode %n2 is object xmlNode %doc = new %doc:loadXml(- '<top> <a>a1</a> <a>a2</a> <a>a3</a> </top>') %nl1 = %doc:SelectNodes('/top/a') %n1 = %nl1:item(2) %n2 = %n1:InsertElementBefore('a', 'a1.5') Call %doc:print end

    The example result follows:

    <top> <a>a1</a> <a>a1.5</a> <a>a2</a> <a>a3</a> </top>

  2. In the following example, SelectNodes is used to create two XmlNodelists, and the nodes in the difference between those nodelists are printed:

    begin %doc is object xmlDoc %n1 is object xmlNode %nl1 is object xmlNodelist %nl2 is object xmlNodelist %nl3 is object xmlNodelist %i is float %doc = new %doc:loadXml('<top><a><a1/></a><a><a2/></a><a><a3/></a></top>') Call %doc:print %nl1 = %doc:selectNodes('/following::node()') print %nl1:count %nl2 = %doc:selectNodes('/top/a') print %nl2:count %nl3 = %nl1:difference(%nl2) for %i from 1 to %nl3:count %n1 = %nl3:item(%i) print %n1:QName end for end

    The example result follows:

    <top> <a> <a1/> </a> <a> <a2/> </a> <a> <a3/> </a> </top> 7 3 top a1 a2 a3

Request-Cancellation Errors

  • XPath is invalid.
  • Insufficient free space exists in CCATEMP.

See also

  • To select a single node, use SelectSingleNode, which stops after selecting a single node.
  • For more information about using XPath expressions, see XPath.