SelectNodes (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 1: Line 1:
{{Template:XmlDoc/XmlNode:SelectNodes subtitle}}
{{Template:XmlDoc/XmlNode:SelectNodes subtitle}}


This function stores in an XmlNodelist the list of nodes selected by an
This function stores in an <var>XmlNodelist</var> the list of nodes selected by an
XPath expression.
XPath expression.
==Syntax==
==Syntax==
Line 8: Line 8:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%nlis</th>
<tr><th>%nlis</th>
<td>An XmlNodelist that is set to point to the nodelist that is created. </td></tr>
<td>An <var>XmlNodelist</var> that is set to point to the nodelist that is created. </td></tr>
<tr><th>nr</th>
<tr><th>nr</th>
<td>An XmlDoc or XmlNode, used as the context node for the <i>XPath</i> expression. If an XmlDoc, the Root node is the context node. </td></tr>
<td>An <var>XmlDoc</var> or <var>XmlNode</var>, used as the context node for the <i>XPath</i> expression. If an <var>XmlDoc</var>, the Root node is the context node. </td></tr>
<tr><th>XPath</th>
<tr><th>XPath</th>
<td>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 (<i>nr</i>).
<td>A <var>Unicode</var> string that is an XPath expression that results in a nodelist. The method returns these nodes in an <var>XmlNodelist</var>. This is an optional argument whose default is a period (.), that is, the node referenced by the method object (<i>nr</i>).
<p class="code">Prior to ''Sirius Mods'' version 7.6, this is an EBCDIC string.</td></tr>
<p class="code">Prior to ''Sirius Mods'' version 7.6, this is an EBCDIC string.</td></tr>
</p>
</p>
Line 21: Line 21:
<li>If the result of the argument XPath expression (<i>XPath</i>) is empty,
<li>If the result of the argument XPath expression (<i>XPath</i>) is empty,
Null is returned.
Null is returned.
<li>The nodes found by SelectNodes are arranged in the resulting nodelist
<li>The nodes found by <var>SelectNodes</var> are arranged in the resulting nodelist
in document order.
in document order.
For some sets selected by XPath expressions
For some sets selected by XPath expressions
Line 31: Line 31:
==Examples==
==Examples==
<ul>
<ul>
<li>In the following example, SelectNodes creates an XmlNodelist, from which an
<li>In the following example, <var>SelectNodes</var> creates an <var>XmlNodelist</var>, from which an
element is selected to insert an element before:
element is selected to insert an element before:
<p class="code">Begin
<p class="code">Begin
%doc is Object XmlDoc
%doc is <var>Object</var> <var>XmlDoc</var>
%nl1 is Object XmlNodelist
%nl1 is <var>Object</var> <var>XmlNodelist</var>
%n1 is Object XmlNode
%n1 is <var>Object</var> <var>XmlNode</var>
%n2 is Object XmlNode
%n2 is <var>Object</var> <var>XmlNode</var>
%doc = New
%doc = New
%doc:LoadXml(-
%doc:LoadXml(-
   '<top> <a>a1</a> <a>a2</a> <a>a3</a> </top>')
   '<top> <a>a1</a> <a>a2</a> <a>a3</a> </top>')
%nl1 = %doc:SelectNodes('/top/a')
%nl1 = %doc:<var>SelectNodes</var>('/top/a')
%n1 = %nl1:item(2)
%n1 = %nl1:item(2)
%n2 = %n1:InsertElementBefore('a', 'a1.5')
%n2 = %n1:InsertElementBefore('a', 'a1.5')
Line 56: Line 56:
</top>
</top>
</p>
</p>
<li>In the following example, SelectNodes creates two XmlNodelists,
<li>In the following example, <var>SelectNodes</var> creates two <var>XmlNodelist</var>s,
and the nodes in the difference between those nodelists are printed:
and the nodes in the difference between those nodelists are printed:
<p class="code">Begin
<p class="code">Begin
%doc is Object XmlDoc
%doc is <var>Object</var> <var>XmlDoc</var>
%n1 is Object XmlNode
%n1 is <var>Object</var> <var>XmlNode</var>
%nl1 is Object XmlNodelist
%nl1 is <var>Object</var> <var>XmlNodelist</var>
%nl2 is Object XmlNodelist
%nl2 is <var>Object</var> <var>XmlNodelist</var>
%nl3 is Object XmlNodelist
%nl3 is <var>Object</var> <var>XmlNodelist</var>
%i is Float
%i is <var>Float</var>
%doc = New
%doc = New
%doc:LoadXml('<top><a><a1/></a><a><a2/></a><a><a3/></a></top>')
%doc:LoadXml('<top><a><a1/></a><a><a2/></a><a><a3/></a></top>')
Call %doc:Print
Call %doc:Print
%nl1 = %doc:SelectNodes('/following::node()')
%nl1 = %doc:<var>SelectNodes</var>('/following::node()')
Print %nl1:Count
Print %nl1:Count
%nl2 = %doc:SelectNodes('/top/a')
%nl2 = %doc:<var>SelectNodes</var>('/top/a')
Print %nl2:Count
Print %nl2:Count
%nl3 = %nl1:Difference(%nl2)
%nl3 = %nl1:Difference(%nl2)

Revision as of 17:46, 25 January 2011

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


This function stores in an XmlNodelist the list of nodes selected by an XPath expression.

Syntax

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

Syntax terms

%nlis 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, 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 ?? refid=docord..

Examples

  • 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>

  • In the following example, SelectNodes creates 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.