SelectNodes (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 20: Line 20:
<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 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>).
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>
</table>
</table>


Line 39: Line 40:
<li>In the following example, SelectNodes creates an XmlNodelist, from which an
<li>In the following example, SelectNodes creates an XmlNodelist, from which an
element is selected to insert an element before:
element is selected to insert an element before:
<pre>
<p class="code">Begin
    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(-
    %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(2)
    %n1 = %nl1:item(2)
%n2 = %n1:InsertElementBefore('a', 'a1.5')
    %n2 = %n1:InsertElementBefore('a', 'a1.5')
Call %doc:Print
    Call %doc:Print
End
    End
</p>
</pre>


The example result follows:
The example result follows:
<pre>
<p class="output"><top>
    <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>
</pre>
<li>In the following example, SelectNodes creates two XmlNodelists,
<li>In the following example, SelectNodes creates two XmlNodelists,
and the nodes in the difference between those nodelists are printed:
and the nodes in the difference between those nodelists are printed:
<pre>
<p class="code">Begin
    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('<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:SelectNodes('/following::node()')
Print %nl1:Count
    Print %nl1:Count
%nl2 = %doc:SelectNodes('/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
End For
    End For
End
    End
</p>
</pre>


The example result follows:
The example result follows:
<pre>
<p class="output"><top>
    <top>
  <a>
      <a>
      <a1/>
          <a1/>
  </a>
      </a>
  <a>
      <a>
      <a2/>
          <a2/>
  </a>
      </a>
  <a>
      <a>
      <a3/>
          <a3/>
  </a>
      </a>
</top>
    </top>
7
    7
3
    3
top
    top
a1
    a1
a2
    a2
a3
    a3
</p>
</pre>
</ul>
</ul>



Revision as of 05:42, 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.