page 47
3.2.6 Axes
To this point, weve been able to select child elements, attributes, text, comments, and processing instructions with some fairly simple XPath expressions. Obviously, we might
want to select many other things, such as:
•
All ancestors of the context node
•
All descendants of the context node
•
All previous siblings or following siblings of the context node siblings are nodes that have the same parent
To select these things, XPath provides a number of axes that let you specify various collections of nodes. There are thirteen axes in all; well discuss all of them here, even though
most of them wont be particularly useful to you. To use an axis in an XPath expression, type the name of the axis, a double colon
::
, and the name of the element you want to select, if any.
Before we define all of the axes, though, we need to talk about XPaths unabbreviated syntax.
3.2.6.1 Unabbreviated syntax
To this point, all the XPath expressions weve looked at used the XPath abbreviated syntax. Most of the time, thats what youll use; however, most of the lesser-used axes can only be
specified with the unabbreviated syntax. For example, when we wrote an XPath expression to select all of the
line
elements in the current context, we used the abbreviated syntax:
xsl:apply-templates select=line
If you really enjoy typing, you can use the unabbreviated syntax to specify that you want all of the
line
children of the current context:
xsl:apply-templates select=child::line
Well go through all of the axes now, pointing out which ones have an abbreviated syntax.
3.2.6.2 Axis roll call
The following list contains all of the axes defined by the XPath standard, with a brief description of each one.
child
axis Contains the children of the context node. As weve already mentioned, the XPath
expression
child::lineschild::line
is equivalent to
linesline
. If an XPath expression such as
sonnet
doesnt have an axis specifier, the
child
axis is used by default. The children of the context node include all comment, element, processing
instruction, and text nodes. Attribute and namespace nodes are not considered children of the context node.
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.
page 48 self
axis Contains the context node itself. The
self
axis can be abbreviated with a single period
.
. The expressions
self::
and
.
are equivalent.
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 parents parent, etc. 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 parents 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. The children are all of the comment, element, processing instruction, and text nodes beneath the context node. In other words, the
descendant
axis does not include attribute or namespace nodes. As we discussed earlier, although an attribute
node has an element node as a parent, an attribute node is not considered a child of that element.
descendant-or-self
axis Contains the context node and all the children of the context node, all the children of
all the children of the context node, all the children of the children of all the children of the context node, and so on. As always, the children of the context node include all
comment, element, processing instruction, and text nodes; attribute and namespace nodes are not included.
preceding-sibling
axis Contains all 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 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
ancestors, attribute nodes, and namespace nodes.
following
axis Contains all nodes that appear after the context node in the document, except
descendants, attribute nodes, and namespace nodes.