SelectSingleNode (XmlDoc/XmlNode function): Difference between revisions
m (1 revision) |
m (→Examples) |
||
Line 28: | Line 28: | ||
attributes are then added: | attributes are then added: | ||
<p class="code">Begin | <p class="code">Begin | ||
%doc is | %doc is Object XmlDoc | ||
%doc = New | %doc = New | ||
%n1 is | %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: | %n1 = %doc:SelectSingleNode('/*/F') | ||
Call %n1:AddAttribute('b', 'b1') | Call %n1:AddAttribute('b', 'b1') | ||
%n1 = %doc: | %n1 = %doc:SelectSingleNode('/*/F[2]/*') | ||
Call %n1:AddAttribute('b', 'b2') | Call %n1:AddAttribute('b', 'b2') | ||
%n1 = %doc: | %n1 = %doc:SelectSingleNode('/*/F[3]/a3') | ||
Call %n1:AddAttribute('b', 'b3') | Call %n1:AddAttribute('b', 'b3') | ||
Call %doc:Print | Call %doc:Print | ||
Line 44: | Line 44: | ||
The example result follows: | The example result follows: | ||
<p class="output"><top> | <p class="output"><top> | ||
<F b="b1"> | |||
<a1/> | |||
</F> | |||
<F> | |||
<a2 b="b2"/> | |||
</F> | |||
<F> | |||
<a3 b="b3"/> | |||
</F> | |||
</top> | </top> | ||
</p> | </p> | ||
Line 64: | Line 64: | ||
use a loop with <var>SelectSingleNode</var> 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: | %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 | ||
Line 73: | Line 73: | ||
then loop through the nodes in that nodelist, as follows: | 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('/*/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 '') | |||
End For | End For | ||
</p> | </p> | ||
Line 88: | Line 86: | ||
For more information about this ordering issue, see [[??]] refid=docord.. | For more information about this ordering issue, see [[??]] refid=docord.. | ||
</ul> | </ul> | ||
=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:54, 7 February 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.