SelectSingleNode (XmlDoc/XmlNode function)
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 ( |
Usage notes
- If the result of the argument XPath expression (XPath) is empty, a 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: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>
- 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
througha10
, and to each of them you want to add ab
attribute whose values are, respectively,b4
throughb10
.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 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 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 “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.