SelectSingleNode (XmlDoc/XmlNode function): Difference between revisions
m (1 revision) |
m (1 revision) |
||
Line 1: | Line 1: | ||
{{Template:XmlDoc/XmlNode:SelectSingleNode subtitle}} | {{Template:XmlDoc/XmlNode:SelectSingleNode subtitle}} | ||
This function stores in an XmlNode the node selected by an | This function stores in an <var>XmlNode</var> the node selected by an | ||
XPath expression. | XPath expression. | ||
==Syntax== | ==Syntax== | ||
Line 8: | Line 8: | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%nod</th> | <tr><th>%nod</th> | ||
<td>An XmlNode, which will be set to point to the selected node. </td></tr> | <td>An <var>XmlNode</var>, which will be set to point to the selected node. </td></tr> | ||
<tr><th>nr</th> | <tr><th>nr</th> | ||
<td>An XmlDoc or XmlNode, used as the context node for the <i>XPath</i> expression. If an XmlDoc, 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 <i>XPath</i> expression. If an <var>XmlDoc</var>, the Root node is the context node. </td></tr> | ||
<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 <var>Unicode</var> 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. | ||
<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 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> | </p> | ||
Line 25: | Line 25: | ||
==Examples== | ==Examples== | ||
<ul> | <ul> | ||
<li>In the following example, SelectSingleNode selects individual nodes to which | <li>In the following example, <var>SelectSingleNode</var> selects individual nodes to which | ||
attributes are then added: | attributes are then added: | ||
<p class="code">Begin | <p class="code">Begin | ||
%doc is Object XmlDoc | %doc is <var>Object</var> <var>XmlDoc</var> | ||
%doc = New | %doc = New | ||
%n1 is Object XmlNode | %n1 is <var>Object</var> <var>XmlNode</var> | ||
%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:<var>SelectSingleNode</var>('/*/F') | ||
Call %n1:AddAttribute('b', 'b1') | Call %n1:AddAttribute('b', 'b1') | ||
%n1 = %doc:SelectSingleNode('/*/F[2]/*') | %n1 = %doc:<var>SelectSingleNode</var>('/*/F[2]/*') | ||
Call %n1:AddAttribute('b', 'b2') | Call %n1:AddAttribute('b', 'b2') | ||
%n1 = %doc:SelectSingleNode('/*/F[3]/a3') | %n1 = %doc:<var>SelectSingleNode</var>('/*/F[3]/a3') | ||
Call %n1:AddAttribute('b', 'b3') | Call %n1:AddAttribute('b', 'b3') | ||
Call %doc:Print | Call %doc:Print | ||
Line 60: | Line 60: | ||
attribute whose values are, respectively, <tt>b4</tt> through <tt>b10</tt>. | attribute whose values are, respectively, <tt>b4</tt> through <tt>b10</tt>. | ||
Instead of a series of SelectSingleNode statements as in the example above, | Instead of a series of <var>SelectSingleNode</var> statements as in the example above, | ||
you might want to | you might want to | ||
use a loop with SelectSingleNode and an XPath pattern like the following: | use a loop with <var>SelectSingleNode</var> and an XPath pattern like the following: | ||
<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:<var>SelectSingleNode</var>('/*/F[' With %i With ']/*') | ||
%n1:AddAttribute('b', 'b' With %i With '') | %n1:AddAttribute('b', 'b' With %i With '') | ||
End For | End For |
Revision as of 17:46, 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.