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

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

The directions of navigation through the tree are called
axes
. The various axes are defined in detail in Chapter 9. They include the following:

  • The child axis, which finds all the children of a node.
  • The attribute axis, which finds all the attributes of a node.
  • The ancestor axis, which finds all the ancestors of a node.
  • The following-sibling axis, which finds the nodes that come after this one and share the same parent.
  • The preceding-sibling axis, which finds the nodes that come before this one and share the same parent.

As well as specifying the direction of navigation through the tree, each step in a path expression can also qualify which nodes are to be selected. This can be done in several different ways:

  • By specifying the name of the nodes (completely or partially).
  • By specifying the kind of nodes (for example, elements or processing instructions).
  • By specifying the schema-defined type of the nodes (for example, elements of type
    person
    , or attributes of type
    xs:date
    ).
  • By defining a predicate that the nodes must satisfy—an arbitrary boolean expression.
  • By defining the relative position of the node along the axis: for example, it is possible to select only the immediately preceding sibling.

The syntax of a path expression uses
/
as an operator to separate the successive steps. A
/
at the start of a path expression indicates that the origin is the document node; otherwise, it is generally the context node (we'll be looking at the notion of context in the next section). Within each step, the axis is written first, separated from the other conditions by the separator
::
. However, the child axis is the default, so it may be omitted; and the attribute axis may be abbreviated to
@
.

For example:

child::item/attribute::category

is a path expression of two steps; the first selects all the child

elements of the current node, and the second selects their
category
attributes. This can be abbreviated to:

item/@category

Predicates that the nodes must satisfy are written in square brackets, for example:

item[@code=‘T’]/@category

This selects the
category
attributes of those child

elements that have a
code
attribute whose value is
T
.

There are many ways of abbreviating path expressions to make them easier to write, but the basic structure remains the same. The full detail appears in Chapter 9.

Context

The way in which XPath expressions are evaluated is to some extent context dependent. For example, the value of the expression
$x
depends on the current value of the variable
x
, and the value of the expression
.
depends on which node is currently being processed in the source document.

Other books

Romance Classics by Peggy Gaddis
King's Folly (Book 2) by Sabrina Flynn
The Google Resume by Gayle Laakmann McDowell
My Struggle: Book 2: A Man in Love by Karl Ove Knausgaard, Don Bartlett
Killer Wedding by Jerrilyn Farmer
Divine Evil by Nora Roberts
Firestorm by Anderson, Taylor