SelectSingleNode (XmlDoc/XmlNode function): Difference between revisions
m (→Examples) |
m (1 revision) |
(No difference)
|
Revision as of 16:34, 8 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.