Book HomeXSLSearch this book

B.3. XPath Axes

The XPath specification defines thirteen different axes; each axis contains various nodes. The nodes that are in a given axis depend on the context node. All 13 axes, excerpted from our more involved discussion in Section 3.1, "The XPath Data Model" in Chapter 3, "XPath: A Syntax for Describing Needles and Haystacks", are listed here.

child axis
Contains the children of the context node. As we've already mentioned, the XPath expressions child::lines/child::line and lines/line are equivalent. If an XPath expression (such as /sonnet) doesn't have an axis specifier, the child axis is used by default.

parent axis
Contains the parent of the context node, if there is one. (If the context node is the root node, the parent axis returns an empty node-set.) This axis can be abbreviated with a double period (..). The expressions parent::sonnet and ../sonnet are equivalent. If the context node does not have a <sonnet> element as its parent, these XPath expressions return an empty node-set.

self axis
Contains the context node itself. The self axis can be abbreviated with a single period (.).

attribute axis
Contains the attributes of the context node. If the context node is not an element node, this axis is empty. The attribute axis can be abbreviated with the at sign (@). The expressions attribute::type and @type are equivalent.

ancestor axis
Contains the parent of the context node, the parent's parent, and so on. The ancestor axis always contains the root node, unless the context node is the root node.

ancestor-or-self axis
Contains the context node, its parent, its parent's parent, and so on. This axis always includes the root node.

descendant axis
Contains all children of the context node, all children of all the children of the context node, and so on. Be aware that the descendant axis does not include any attribute or namespace nodes. (As we discussed earlier, an attribute node has an element node as its parent, even though the attribute node is not considered a child of its parent.)

descendant-or-self axis
Contains the context node and all children of the context node, all children of all the children of the context node, and so on.

preceding-sibling axis
Contains all of the preceding siblings of the context node -- in other words, all nodes that have the same parent as the context node and appear before the context node in the XML document. If the context node is an attribute node or a namespace node, the preceding-sibling axis is empty.

following-sibling axis
Contains all of the following siblings of the context node -- in other words, all nodes that have the same parent as the context node and appear after the context node in the XML document. If the context node is an attribute node or a namespace node, the following-sibling axis is empty.

preceding axis
Contains all nodes that appear before the context node in the document, except any ancestors, attribute nodes, and namespace nodes.

following axis
Contains all nodes that appear after the context node in the document, except any descendants, attribute nodes, and namespace nodes.

namespace axis
Contains the namespace nodes of the context node. If the context node is not an element node, this axis is empty.

The five axes ancestor, descendant, following, preceding, and self partition everything in the XML document (with the exception of any attribute or namespace nodes). Any node in the XPath tree appears in one of these five axes, and the five axes do not overlap.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.