XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (392 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.95Mb size Format: txt, pdf, ePub

Axes

An
axis
is a path through the document tree, starting at a particular node (which I'll call the origin) and following a particular relationship between nodes. There are 13 axes defined in XPath, as follows:

  • ancestor
  • ancestor-or-self
  • attribute
  • child
  • descendant
  • descendant-or-self
  • following
  • following-sibling
  • namespace
  • parent
  • preceding
  • preceding-sibling
  • self

This section explains the meaning of each of the axes, giving both a textual definition and a diagram.

The diagram shows the origin node in dark shading, while the nodes on the axis are numbered in the sequence they appear on the axis. The diagram does not show attribute and namespace nodes.

Description
Diagram
ancestor
Selects all the nodes that are ancestors of the origin node. The first node on the axis is the parent of the origin node, the second is its grandparent, and so on; the last node on the axis is the root of the tree.
ancestor-or-self
Selects the same nodes as the ancestor axis, but starting with the origin node rather than with its parent.
attribute
If the origin node is an element, this axis selects all its attribute nodes. Otherwise, it selects nothing (an empty sequence). The result is in document order; however, document order for attributes is arbitrary and unpredictable. The attributes won't necessarily be in the order in which they appeared in the original lexical XML, and the order may vary from one XSLT processor to another. Note also that namespace declarations are not treated as attribute nodes and will never be selected by the attribute axis.
child
Selects all the children of the origin node, in document order. For any node except a document node or element node, this selects nothing. Note that the children of an element node do not include its attributes or namespace nodes, only the text nodes, element nodes, processing instructions, and comments that make up its content.
descendant
Selects all the children of the origin node, and their children, and so on recursively. The resulting nodes are in document order. If the origin is an element, this effectively means that the descendant axis contains all the text nodes, element nodes, comments and processing instructions that appear in the original source document between that element's start and end tags, in their original sequence.
descendant-or-self
This is the same as the descendant axis, except that the first node selected is the origin node itself.
following
This selects all the nodes that appear after the origin node in document order, excluding the descendants of the origin node. If the origin is an element node, for example, this effectively means that it contains all the text nodes, element nodes, comments and processing instructions in the document that start after the end tag of the origin element. The following axis will never contain attribute or namespace nodes.
following-sibling
This selects all the nodes that follow the origin node in document order, and that are children of the same parent node. If the origin is a document node, an attribute node, or a namespace node, then the following-sibling axis will always be empty.
namespace
If the origin node is an element, this axis selects all the namespace nodes that are in scope for that element; otherwise, it is empty. The order of the namespace nodes is undefined. The namespace nodes correspond to namespace declarations (
xmlns=“x”
or
xmlns:y=“z”
) on the element itself or on one of its ancestor elements, but excluding any namespace declaration that cannot be used on this element because it is masked by another declaration of the same namespace prefix, or because it is undeclared (XML 1.1 only). For more information about namespace nodes see Chapter 2.
parent
This axis selects a single node, the parent of the origin node. If the origin node is a document node, or any other node that happens to be the root of a tree, then the parent axis is empty.
preceding
This selects all the nodes that appear before the origin node, excluding the ancestors of the origin node. If the origin is an element node, this effectively means that it contains all the text nodes, element nodes, comments and processing instructions in the document that finish before the start tag of the origin element. The preceding axis will never contain attribute or namespace nodes.
preceding-sibling
This selects all the nodes that precede the origin node, and that are children of the same parent node. If the origin is a document node, an attribute node, or a namespace node, then the preceding-sibling axis will always be empty.
self
This selects a single node, the origin node itself. This axis will never be empty. The self axis is generally used with a node test as a way of testing whether the context node conforms to that node test: for example, the filter expression
$nodes[self::para]
selects items in
$nodes
that are

elements. This could equally be written
$nodes/self::para
.

Other books

Tate by Barbara S. Stewart
Persuasion by Owner
In the After by Demitria Lunetta
Snowscape Trilogy by Jessie Lyn Pizanias
Taking Liberty by Jodi Redford
One Chance by T. Renee Fike