This chapter is based on Chapter 6 of Erik Ray's book [Ray 2001], Chapter 11 of the Deitel, et.al. tome [Deitel 2001], Chapter 14 of Elliotte Rusty Harold's book [Harold 1999], plus additional material from the web.
In the previous chapter on XSL and XSLT, the definition of what template was matched or what value was tested was left deliberately vague. This is not only because the documents were relatively simple, but because this topic, known collectively as XPath, is a sophisticated language for marking locations and selecting nodes within a document.
The location of a node, or a group of nodes, somewhere in a document is called a location path. It can be either absolute, starting from the root node, or relative, which starts at a variable point called the context node.
A location path consists of a series of steps, each of which is composed of an axis, describing the direction of travel, a node test, which specifies what nodes are applicable, and an optional predicate, that uses Boolean tests to narrow down the selection. Figure 1 lists the types of axes.
| Axis | Ordering | Description |
|---|---|---|
ancestor
|
Reverse | Context node's ancestors |
ancestor-or-self
|
Reverse | Context node's ancestors and itself |
attribute
|
Forward | Attributes of the context node |
child
|
Forward | Context node's children |
descendant
|
Forward | Context node's descendants |
descendant-or-self
|
Forward | Context node's descendants and itself |
following
|
Forward | Nodes following the context node |
following-sibling
|
Forward | Nodes following the context node that share the same parent as the context node |
namespace
|
Forward | Namespace nodes of the context node |
parent
|
Reverse | Context node's parent |
preceeding
|
Reverse | Nodes preceeding the context node |
preceeding-sibling
|
Reverse | Nodes preceeding the context node that share the same parent as the context node |
self
|
None | Context node itself |
After the axis comes a node test joined to the axis by a
double colon (::). Figure 2 lists the node
tests.
| Node test | Description |
|---|---|
/
|
The root node, not the root element |
| name | Nodes with that name |
*
|
Nodes of the same principal node type |
comment()
|
Comment nodes |
node()
|
Nodes (except attributes and namespace declarations) |
processing-instruction()
|
Processing instruction nodes |
text()
|
Text nodes |
The location step child::* selects all
element-node children of the context node because the
principle node type for the child axis is
element. The location step child::text()
selects all text-node children of the context node. These
two location steps can be combined with the
chaining operator slash (/) to form the
location path child::*/child::text()
which selects all text-node grandchildren of the context
node.
Some location paths can be abbreviated. Figure 3 lists the possibilities.
| Location Path | Description |
|---|---|
attribute::
|
The attribute axis may be abbreviated as @
|
child::
|
The default location path if no axis is supplied |
/descendant-or-self::node()/
|
This location path may be abbreviated as two slashes
(//)
|
parent::node()
|
The context node's parent may be abbreviated as two periods
(..)
|
self::node()
|
The context node may be abbreviated as a period
(.)
|
It is now time to look at some examples. Consider Mick's XSL Tree and assume that
the context node is the second (and last)
ingredient element node. The examples are
given in Figure 4.
| Location Path | Description |
|---|---|
comment()
|
The comment about the bread |
/comment()
|
The comment about Mick's favourite |
//comment()
|
Both comments |
text()
|
The text "Bread"
|
/*
|
The document element |
../@*
|
All attributes of the sandwich element
|
//ingredient
|
All ingredient nodes
|
following-sibling::*
|
Nothing, because the context node is the last child of its parent |
If the axis and node type are not sufficient to select the
correct set, then one or more predicates can be used. A
predicate is a boolean expression contained in square braces
([...]); nodes that pass this test are included
in the final node set. Figure 5 shows some fictitious
examples.