SelectNodes (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (xpath expression)
 
m (edits, tags and links)
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 <var>[[XPath#XPath_syntax|Xpath expression]]</var>.
This function stores in an <var>XmlNodelist</var> the list of nodes selected by an <var>[[XPath#XPath_syntax|Xpath expression]]</var>.


==Syntax==
==Syntax==
Line 8: 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 set to point to the <var>nodelist</var> that 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 Root 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>
<tr><th>xpath</th>
<tr><th>xpath</th>
<td>A <var>Unicode</var> string that is an <var>[[XPath#XPath_syntax|Xpath expression]]</var> 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>).
<td>A <var>Unicode</var> string that is an <var>[[XPath#XPath_syntax|Xpath expression]]</var> that results in a <var>nodelist</var>. The method returns these nodes in an <var>XmlNodelist</var>. This is an optional argument whose default is a period (<code>.</code>), that is, the node referenced by the method object (<var class="term">nr</var>).
 
<p>Prior to <var class="product">[[Sirius Mods|"Sirius Mods"]]</var> Version 7.6, this is an EBCDIC string.</p></td></tr>
Prior to ''Sirius Mods'' version 7.6, this is an EBCDIC string.</td></tr>
 
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If the result of the argument XPath expression (<i>XPath</i>) is empty,
<li>If the result of the argument <var>XPath</var> expression (<var class="term">XPath</var>) is empty, a <var>Null</var> is returned.
Null is returned.
<li>The nodes found by <var>SelectNodes</var> are arranged in the resulting <var>nodelist</var> in document order. For some sets selected by <var>XPath</var> expressions (for example, the set of ancestor nodes),
<li>The nodes found by <var>SelectNodes</var> are arranged in the resulting nodelist
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”]].
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..
</ul>
</ul>


==Examples==
==Examples==
<ul>
<ol>
<li>In the following example, <var>SelectNodes</var> creates an <var>XmlNodelist</var>, 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 Object XmlDoc
  %nl1 is object xmlNodelist
%nl1 is Object XmlNodelist
  %n1 is object xmlNode
%n1 is Object XmlNode
  %n2 is object xmlNode
%n2 is Object XmlNode
  %doc = new
%doc = New
  %doc:[[LoadXml_(XmlDoc/XmlNode_function)|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:SelectNodes('/top/a')
  %n1 = %nl1:[[Item_(XmlNodelist_function)|item]](2)
%n1 = %nl1:item(2)
  %n2 = %n1:[[InsertElementBefore (XmlNode function)|InsertElementBefore]]('a', 'a1.5')
%n2 = %n1:InsertElementBefore('a', 'a1.5')
  [[Notation_conventions_for_methods#Callable_methods|Call]] %doc:print
Call %doc:Print
end
End
</p>
</p>
The example result follows:
The example result follows:
<p class="output"><top>
<p class="output"><top>
Line 57: Line 47:
</top>
</top>
</p>
</p>
<li>In the following example, <var>SelectNodes</var> creates two <var>XmlNodelist</var>s,
<li>In the following example, <var>SelectNodes</var> is used to create 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 Object XmlDoc
  %n1 is object xmlNode
%n1 is Object XmlNode
  %nl1 is object xmlNodelist
%nl1 is Object XmlNodelist
  %nl2 is object xmlNodelist
%nl2 is Object XmlNodelist
  %nl3 is object xmlNodelist
%nl3 is Object XmlNodelist
  %i is float
%i is Float
  %doc = new
%doc = New
  %doc:[[LoadXml_(XmlDoc/XmlNode_function)|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>')
  [[Notation_conventions_for_methods#Callable_methods|Call]] %doc:print
Call %doc:Print
  %nl1 = %doc:selectNodes('/following::node()')
%nl1 = %doc:SelectNodes('/following::node()')
  print %nl1:[[Count_(XmlNodelist_function)|count]]
Print %nl1:Count
  %nl2 = %doc:selectNodes('/top/a')
%nl2 = %doc:SelectNodes('/top/a')
  print %nl2:count
Print %nl2:Count
  %nl3 = %nl1:[[Difference_(XmlNodelist_function)|difference]](%nl2)
%nl3 = %nl1:Difference(%nl2)
  for %i from 1 to %nl3:count
For %i from 1 to %nl3:Count
      %n1 = %nl3:[[Item_(XmlNodelist_function)|item]](%i)
  %n1 = %nl3:Item(%i)
      print %n1:[[QName (XmlDoc/XmlNode function)|QName]]
  Print %n1:qName
  end for
End For
end
End
</p>
</p>
The example result follows:
The example result follows:
<p class="output"><top>
<p class="output"><top>
Line 100: Line 88:
a3
a3
</p>
</p>
</ul>
</ol>


==Request-Cancellation Errors==
==Request-Cancellation Errors==
<ul>
<ul>
<li><i>XPath</i> is invalid.
<li><var class="term">XPath</var> is invalid.
<li>Insufficient free space exists in CCATEMP.
<li>Insufficient free space exists in CCATEMP.
</ul>
</ul>


==See also==
==See also==
{{Template:XmlDoc/XmlNode:SelectNodes footer}}
<ul>
<ul>
<li>To select a single node, use
<li>To select a single node, use <var>[[SelectSingleNode (XmlDoc/XmlNode function)|SelectSingleNode]]</var>, which stops after selecting a single node.
??[[SelectSingleNode (XmlDoc/XmlNode function)|SelectSingleNode]], which stops
after selecting a single node.
<li>For more information about using XPath expressions, see [[XPath]].
<li>For more information about using XPath expressions, see [[XPath]].
</ul>
</ul>
{{Template:XmlDoc/XmlNode:SelectNodes footer}}

Revision as of 09:41, 22 May 2011

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.