9.9. AxisAlthough it's not usually associated with evil (although cursing is a different story), an axis is a node set starting at a particular node that is based on the relationship between the nodes in an XML document. The basic format for using an axis follows: axis::context-node Table 9-3 describes the properties of the various axes available in XPath.
The use of an axis is arguably the most formidable concept for developers new to XPath, who often have difficulty trying to visualize the results of using an axis. Fortunately, tools such as the XPath Evaluator in Altova's XMLSPY make it easier to see the results of specifying a particular axis. Starting with the original XML document from Listing 9-1, the following sections present examples of each of the various axes. 9.9.1. Ancestor Axis ExampleXPath Statement//book[3]/ancestor::* Result Node Setlibrary ExplanationBecause the context node, the third book node, is a child of the root element, there is only a single ancestor. 9.9.2. ancestor-or-self Axis ExampleXPath Statement//book[3]/ancestor-or-self::* Result Node Setbook ExplanationIn addition to the ancestor nodes, the ancestor-or-self axis returns the context node. Also, because the results are in reverse document order, the context node is the first node in the node set, followed by the parent node and so on up the tree. 9.9.3. attribute Axis ExampleXPath Statement//book[3]/attribute::* Result Node Setpublisher ExplanationBecause the context node has only one attribute, it is the only attribute returned in the node set. 9.9.4. child Axis ExampleXPath Statement//book[3]/child::* Result Node Setseries "The Lord of the Rings" ExplanationThe resulting node set consists of the three child nodes of the context node. I have shown the contents of the individual nodes to distinguish these nodes from similar nodes with different contents. 9.9.5. descendant Axis ExampleXPath Statement//book[3]/descendant::* Result Node Setseries "The Lord of the Rings" ExplanationThe results shown here are identical to the results from the child axis. This is because of the structure of the XML document. For instance, if any of the child nodes shown here had children of their own, the descendant axis would have returned their children, and so on down the line in document order, whereas the child axis would not. 9.9.6. descendant-or-self Axis ExampleXPath Statement//book[3]/descendant-or-self::* Result Node Setbook ExplanationAs with the descendant axis, all child nodes are returned recursively. However, instead of starting with the first child, the context node is the first node in the node set. 9.9.7. following Axis ExampleXPath Statement//book[3]/following::* Result Node Setbook ExplanationThe resulting node set for the following axis is always all the nodes that occur after the context node in document order. 9.9.8. following-sibling Axis ExampleXPath Statement//book[3]/following-sibling::* Result Node Setbook ExplanationThese five book nodes retrieved using the following-sibling axis are the same nodes that were retrieved by the following axis. The only difference is that the following-sibling axis retrieves only those nodes on the same level as the context node and have the same parent as the context node. 9.9.9. namespace Axis ExampleXPath Statement//book[3]/namespace::* Result Node SetEmpty node set ExplanationBecause no namespace was in scope on the context node, the resulting node set is empty. However, if one or more namespaces had been in scope, the resulting node set would have contained those in scope. 9.9.10. parent Axis ExampleXPath Statement//book[3]/parent::* Result Node Setlibrary ExplanationThe resulting node set will always consist of either an empty node set or a single node. For example, the parent axis of the library element would have retrieved an empty node set. 9.9.11. preceding Axis ExampleXPath Statement//book[3]/preceding::* Result Node Setauthor "J.R.R. Tolkien" ExplanationThe resulting node set of the preceding axis is made up of those nodes that occur in the XML document before the context node, in reverse document order. 9.9.12. preceding-sibling Axis ExampleXPath Statement//book[3]/preceding-sibling::* Result Node Setbook ExplanationThese book nodes retrieved using the preceding-sibling axis are the same nodes that were retrieved by the preceding axis. However, the difference is that the preceding-sibling axis retrieves only those nodes that are on the same level as the context node and that have the same parent as the context node. 9.9.13. self Axis ExampleXPath Statement//book[3]/self::* Result Node Setbook ExplanationThe self axis returns the context node; essentially, the result is the same as if the axis were omitted. |
Friday, October 16, 2009
Section 9.9. Axis
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment