SelectNodes (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{Template:XmlDoc/XmlNode:SelectNodes subtitle}}
{{Template:XmlDoc/XmlNode:SelectNodes subtitle}}
<var>SelectNodes</var> stores, in an <var>XmlNodelist</var>, the list of nodes selected by an [[XPath#XPath_syntax|XPath expression]].
<var>SelectNodes</var> creates an <var>XmlNodelist</var> containing nodes selected by an [[XPath#XPath_syntax|XPath expression]].


==Syntax==
==Syntax==
Line 7: Line 7:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%nodl</th>
<tr><th>%nodl</th>
<td>An <var>XmlNodelist</var> that is set to point to the nodelist that is created.</td></tr>
<td>An <var>XmlNodelist</var> that is created, containing the nodes selected by the specified ''xpath''.  If the selection results in no found nodes, an empty XMLNodelist is created.</td></tr>
<tr><th>nr</th>
<tr><th>nr</th>
<td>An <var>XmlDoc</var> or <var>XmlNode</var>, used as the context node for the <var class="term">xpath</var> expression. If an <var>XmlDoc</var>, the <var>Root</var> node is the context node.</td></tr>
<td>An <var>XmlDoc</var> or <var>XmlNode</var>, used as the context node for the <var class="term">xpath</var> expression. If an <var>XmlDoc</var>, the <var>Root</var> node is the context node.</td></tr>
Line 17: Line 17:
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If the result of the <var class="term">xpath</var> argument XPath expression is empty, a <var>Null</var> is returned.
<li>If the result of the <var class="term">xpath</var> argument XPath expression is empty, an empty XMLNodelist is returned.
<li>The nodes found by <var>SelectNodes</var> are arranged in the resulting nodelist in document order. For some sets selected by XPath expressions (for example, the set of ancestor nodes),
<li>The nodes found by <var>SelectNodes</var> 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 [[XPath#Order_of_nodes:_node_sets_versus_nodelists|“Order of nodes: node sets versus nodelists”]].
document order may not be the ordering you intuitively expect. For a discussion of this issue, see [[XPath#Order_of_nodes:_node_sets_versus_nodelists|“Order of nodes: node sets versus nodelists”]].

Revision as of 21:22, 9 January 2012

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

SelectNodes creates an XmlNodelist containing nodes selected by an XPath expression.

Syntax

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

Syntax terms

%nodl An XmlNodelist that is created, containing the nodes selected by the specified xpath. If the selection results in no found nodes, an empty XMLNodelist 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 xpath argument XPath expression is empty, an empty XMLNodelist 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

  • The xpath expression 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".