SelectSingleNode (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (xpath expression)
mNo edit summary
Line 1: Line 1:
{{Template:XmlDoc/XmlNode:SelectSingleNode subtitle}}
{{Template:XmlDoc/XmlNode:SelectSingleNode subtitle}}
<var>SelectSingleNode</var> stores, in an <var>xmlNode</var>, the node selected by an <var>[[XPath#XPath_syntax|Xpath]]</var> expression.
<var>SelectSingleNode</var> stores, in an <var>xmlNode</var>, the node selected by an [[XPath]] expression.


==Syntax==
==Syntax==
Line 11: Line 11:
<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>
<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 <var>nodelist</var>. The first node ('''in document order''') of the nodes found by the expression is returned by the method.
<td>A <var>Unicode</var> string that is an [[XPath#XPath_syntax|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.
<p>This is an optional argument whose default is a period (<code>.</code>) value: that is, the node referenced by the method object (<var class="term">nr</var>). Prior to <var class="product">[[Sirius Mods|"Sirius Mods"]]</var> Version 7.6, this argument is an EBCDIC string.</p></td></tr>
<p>This is an optional argument whose default is a period (<tt>.</tt>) value: that is, the node referenced by the method object (<var class="term">nr</var>). Prior to <var class="product">[[Sirius Mods]]</var> Version 7.6, this argument is an EBCDIC string.</p></td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If the result of the argument <var>XPath</var> expression (<var class="term">XPath</var>) is empty, a <var>Null</var> is returned.
<li>If the result of the argument XPath expression (<var class="term">xpath</var>) is empty, a <var>Null</var> is returned.
</ul>
</ul>


Line 54: Line 54:
<p class="code">for %i from 2 to 10
<p class="code">for %i from 2 to 10
   %n1 = %doc:selectSingleNode('/*/F[' with %i with ']/*')
   %n1 = %doc:selectSingleNode('/*/F[' with %i with ']/*')
   %n1:[[AddAttribute_(XmlNode_function)|AddAttribute]]('b', 'b' with %i with '')
   %n1:addAttribute('b', 'b' with %i with '')
end For
end For
</p>
</p>
Alternatively, you might want to gather the third-level nodes into a <var>nodelist</var> (using <var>[[SelectNodes_(XmlDoc/XmlNode_function)|selectNodes]]</var>), then loop through the nodes in that <var>nodelist</var>, as follows:
Alternatively, you might want to gather the third-level nodes into an <var>XmlNodelist</var> (using <var>[[SelectNodes_(XmlDoc/XmlNode_function)|SelectNodes]]</var>), then loop through the nodes in that nodelist, as follows:
<p class="code">%nl1 is object xmlnodelist
<p class="code">%nl1 is object xmlnodelist
...
...
%nl1 = %doc:[[SelectNodes_(XmlDoc/XmlNode_function)|selectNodes]]('/*/F/*')
%nl1 = %doc:[[SelectNodes_(XmlDoc/XmlNode_function)|selectNodes]]('/*/F/*')
for %i from 2 to %nl1:[[Count_(XmlNodelist_function)|count]]
for %i from 2 to %nl1:[[Count_(XmlNodelist_function)|count]]
   [[Notation_conventions_for_methods#Callable_methods|Call]] %nl1:[[Item_(XmlNodelist_function)|item]](%i):[[AddAttribute_(XmlNode_function)|AddAttribute]]('b', 'b' with %i with '')
   [[Notation_conventions_for_methods#Callable_methods|Call]] %nl1:[[Item_(XmlNodelist_function)|item]](%i):AddAttribute('b', 'b' with %i with '')
end for
end for
</p>
</p>
This approach requires a less-creative <var>XPath</var> expression, and it has the advantage that <var>nodelist</var>s are always arranged in document order, whereas <var>XPath</var> selections in some cases
This approach requires a less-creative XPath expression, and it has the advantage that <var>XmlNodelist</var>s 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 [[XPath#Order_of_nodes:_node_sets_versus_nodelists|“Order of nodes: node sets versus nodelists”]].
(although not in this one) use the reverse of document order. For more information about this ordering issue, see [[XPath#Order_of_nodes:_node_sets_versus_nodelists|“Order of nodes: node sets versus nodelists”]].
</ol>
</ol>


==Request-Cancellation Errors==
==Request-Cancellation Errors==
<ul>
<ul>
<li><var class="term">XPath</var> 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>
Line 78: Line 78:
<ul>
<ul>
<li>To select a list of nodes, use <var>[[SelectNodes (XmlDoc/XmlNode function)|SelectNodes]]</var>.
<li>To select a list of nodes, use <var>[[SelectNodes (XmlDoc/XmlNode function)|SelectNodes]]</var>.
<li>For more information about using <var>XPath</var> expressions, see <var>[[XPath]]</var>.
<li>For more information about using <var>XPath</var> expressions, see [[XPath|"XPath"]].
</ul>
</ul>
{{Template:XmlDoc/XmlNode:SelectSingleNode footer}}
{{Template:XmlDoc/XmlNode:SelectSingleNode footer}}

Revision as of 23:44, 23 May 2011

Get selected node as XmlNode (XmlDoc and XmlNode classes)

SelectSingleNode 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, a Null is returned.

Examples

  1. 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:aelectSingleNode('/*/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>

  2. 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 an XmlNodelist (using SelectNodes), 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 XmlNodelists 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 “Order of nodes: node sets versus nodelists”.

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".