SelectNodes (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
Line 34: Line 34:
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 <var>Object</var> <var>XmlDoc</var>
%doc is Object XmlDoc
%nl1 is <var>Object</var> <var>XmlNodelist</var>
%nl1 is Object XmlNodelist
%n1 is <var>Object</var> <var>XmlNode</var>
%n1 is Object XmlNode
%n2 is <var>Object</var> <var>XmlNode</var>
%n2 is Object XmlNode
%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>')
</p>
%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 51: Line 50:
The example result follows:
The example result follows:
<p class="output"><top>
<p class="output"><top>
<a>a1</a>
  <a>a1</a>
<a>a1.5</a>
  <a>a1.5</a>
<a>a2</a>
  <a>a2</a>
<a>a3</a>
  <a>a3</a>
</top>
</top>
</p>
</p>
Line 60: Line 59:
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 <var>Object</var> <var>XmlDoc</var>
%doc is Object XmlDoc
%n1 is <var>Object</var> <var>XmlNode</var>
%n1 is Object XmlNode
%nl1 is <var>Object</var> <var>XmlNodelist</var>
%nl1 is Object XmlNodelist
%nl2 is <var>Object</var> <var>XmlNodelist</var>
%nl2 is Object XmlNodelist
%nl3 is <var>Object</var> <var>XmlNodelist</var>
%nl3 is Object XmlNodelist
%i is <var>Float</var>
%i is Float
%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:<var>SelectNodes</var>('/following::node()')
%nl1 = %doc:SelectNodes('/following::node()')
Print %nl1:Count
Print %nl1:Count
%nl2 = %doc:<var>SelectNodes</var>('/top/a')
%nl2 = %doc:SelectNodes('/top/a')
Print %nl2:Count
Print %nl2:Count
%nl3 = %nl1:Difference(%nl2)
%nl3 = %nl1:Difference(%nl2)
For %i from 1 to %nl3:Count
For %i from 1 to %nl3:Count
%n1 = %nl3:Item(%i)
  %n1 = %nl3:Item(%i)
Print %n1:qName
  Print %n1:qName
</p>
End For
End For
End
End
Line 84: Line 82:
The example result follows:
The example result follows:
<p class="output"><top>
<p class="output"><top>
<a>
  <a>
    <a1/>
      <a1/>
</a>
  </a>
<a>
  <a>
    <a2/>
      <a2/>
</a>
  </a>
<a>
  <a>
    <a3/>
      <a3/>
</a>
  </a>
</p>
</top>
</top>
7
7
Line 104: Line 101:
</ul>
</ul>


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


==See also==
==See also==

Revision as of 19:58, 7 February 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.