SelectSingleNode (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 first node ('''in document order''') of the nodes found by the expression is returned by the method.
<td>A Unicode string that is an XPath expression that results in a nodelist. The first node ('''in document order''') of the nodes found by the expression is returned by the method.
This is an optional argument whose default is a period (.) value: that is, the node referenced by the method object (<i>nr</i>). Prior to ''Sirius Mods'' version 7.6, this argument is an EBCDIC string.</td></tr>
<p class="code">This is an optional argument whose default is a period (.) value: that is, the node referenced by the method object (<i>nr</i>). Prior to ''Sirius Mods'' version 7.6, this argument is an EBCDIC string.</td></tr>
</p>
</table>
</table>


Line 33: Line 34:
<li>In the following example, SelectSingleNode selects individual nodes to which
<li>In the following example, SelectSingleNode selects individual nodes to which
attributes are then added:
attributes are then added:
<pre>
<p class="code">Begin
    Begin
%doc is Object XmlDoc
    %doc is Object XmlDoc
%doc = New
    %doc = New
%n1 is Object XmlNode
    %n1 is Object XmlNode
%doc:LoadXml('<top><F><a1/></F><F><a2/></F><F><a3/></F></top>')
    %doc:LoadXml('<top><F><a1/></F><F><a2/></F><F><a3/></F></top>')
%n1 = %doc:SelectSingleNode('/*/F')
    %n1 = %doc:SelectSingleNode('/*/F')
Call %n1:AddAttribute('b', 'b1')
    Call %n1:AddAttribute('b', 'b1')
%n1 = %doc:SelectSingleNode('/*/F[2]/*')
    %n1 = %doc:SelectSingleNode('/*/F[2]/*')
Call %n1:AddAttribute('b', 'b2')
    Call %n1:AddAttribute('b', 'b2')
%n1 = %doc:SelectSingleNode('/*/F[3]/a3')
    %n1 = %doc:SelectSingleNode('/*/F[3]/a3')
Call %n1:AddAttribute('b', 'b3')
    Call %n1:AddAttribute('b', 'b3')
Call %doc:Print
    Call %doc:Print
End
    End
</p>
</pre>


The example result follows:
The example result follows:
<pre>
<p class="output"><top>
    <top>
  <F b="b1">
      <F b="b1">
      <a1/>
          <a1/>
  </F>
      </F>
  <F>
      <F>
      <a2 b="b2"/>
          <a2 b="b2"/>
  </F>
      </F>
  <F>
      <F>
      <a3 b="b3"/>
          <a3 b="b3"/>
  </F>
      </F>
</top>
    </top>
</p>
</pre>
<li>As a variation on the previous example, say the XML document is the same
<li>As a variation on the previous example, say the XML document is the same
as in that example, except it contains additional third-level nodes <tt>a4</tt>
as in that example, except it contains additional third-level nodes <tt>a4</tt>
Line 71: Line 70:
you might want to
you might want to
use a loop with SelectSingleNode and an XPath pattern like the following:
use a loop with SelectSingleNode and an XPath pattern like the following:
<pre>
<p class="code">For %i From 2 To 10
    For %i From 2 To 10
  %n1 = %doc:SelectSingleNode('/*/F[' With %i With ']/*')
      %n1 = %doc:SelectSingleNode('/*/F[' With %i With ']/*')
  %n1:AddAttribute('b', 'b' With %i With '')
      %n1:AddAttribute('b', 'b' With %i With '')
End For
    End For
</p>
</pre>


Alternatively, you might want to gather the third-level nodes into a nodelist
Alternatively, you might want to gather the third-level nodes into a nodelist
(using the SelectNodes method),
(using the SelectNodes method),
then loop through the nodes in that nodelist, as follows:
then loop through the nodes in that nodelist, as follows:
<pre>
<p class="code">%nl1 is object xmlnodelist
    %nl1 is object xmlnodelist
...
    ...
%nl1 = %doc:SelectNodes('/*/F/*')
    %nl1 = %doc:SelectNodes('/*/F/*')
For %i From 2 To %nl1:Count
    For %i From 2 To %nl1:Count
Call %nl1:Item(%i):AddAttribute('b', 'b' With %i With '')
    Call %nl1:Item(%i):AddAttribute('b', 'b' With %i With '')
End For
    End For
</p>
</pre>


This approach requires a less-creative XPath expression, and it
This approach requires a less-creative XPath expression, and it

Revision as of 05:42, 25 January 2011

Get selected node as XmlNode (XmlDoc and XmlNode classes)

This function stores in an XmlNode the node selected by an XPath expression.

Syntax

%nod = nr:SelectSingleNode[( [xpath])] Throws XPathError

Syntax terms

%nod An XmlNode, which will be set to point to the selected node.
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 first node (in document order) of the nodes found by the expression is returned by the method.

This is an optional argument whose default is a period (.) value: that is, the node referenced by the method object (nr). Prior to Sirius Mods version 7.6, this argument is an EBCDIC string.

Usage notes

  • If the result of the argument XPath expression (XPath) is empty, Null is returned.

Examples

  • In the following example, SelectSingleNode selects individual nodes to which attributes are then added:

    Begin %doc is Object XmlDoc %doc = New %n1 is Object XmlNode %doc:LoadXml('<top><F><a1/></F><F><a2/></F><F><a3/></F></top>') %n1 = %doc:SelectSingleNode('/*/F') Call %n1:AddAttribute('b', 'b1') %n1 = %doc:SelectSingleNode('/*/F[2]/*') Call %n1:AddAttribute('b', 'b2') %n1 = %doc:SelectSingleNode('/*/F[3]/a3') Call %n1:AddAttribute('b', 'b3') Call %doc:Print End

    The example result follows:

    <top> <F b="b1"> <a1/> </F> <F> <a2 b="b2"/> </F> <F> <a3 b="b3"/> </F> </top>

  • As a variation on the previous example, say the XML document is the same as in that example, except it contains additional third-level nodes a4 through a10, and to each of them you want to add a b attribute whose values are, respectively, b4 through b10. Instead of a series of SelectSingleNode statements as in the example above, you might want to use a loop with SelectSingleNode and an XPath pattern like the following:

    For %i From 2 To 10 %n1 = %doc:SelectSingleNode('/*/F[' With %i With ']/*') %n1:AddAttribute('b', 'b' With %i With ) End For

    Alternatively, you might want to gather the third-level nodes into a nodelist (using the SelectNodes method), then loop through the nodes in that nodelist, as follows:

    %nl1 is object xmlnodelist ... %nl1 = %doc:SelectNodes('/*/F/*') For %i From 2 To %nl1:Count Call %nl1:Item(%i):AddAttribute('b', 'b' With %i With ) End For

    This approach requires a less-creative XPath expression, and it has the advantage that nodelists are always arranged in document order, whereas XPath selections in some cases (although not in this one) use the reverse of document order. For more information about this ordering issue, see ?? refid=docord..

Request-Cancellation Errors

  • XPath is invalid.
  • Insufficient free space exists in CCATEMP.


See also

  • To select a list of nodes, use ??SelectNodes.
  • For more information about using XPath expressions, see XPath.