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

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

The following table shows some examples of positional predicates.

Expression
Description
para[1]
Selects the first

child element of the context node.
para[last()]
Selects the last

child element of the context node.
para[position()! = 1]
Selects all

child elements of the context node, other than the first.
para[position() = 1 to 5]
Selects the first five

elements. This works because the
=
operator returns
true
if the left-hand operand (
position()
) contains a value that is equal to one of the items in the right-hand operand (
1 to 5
), which is true if
position()
is in the range
1 to 5
.
para[last()-1]
Returns the last but one

child of the context node.
para[3.2]
Returns an empty sequence. The value 3.2 is treated as a numeric predicate. The value of
position()
will never be equal to 3.2, so no elements are selected.
para[position()]
Selects all child

elements. The predicate expands to
[position()= position()]
, which is always
true
.
para[position()-1]
Returns an empty sequence. The predicate expands to
[position() = position()-1]
, which is always
false
.
para[number(@nr)]
Returns every child

element that has a
nr
attribute whose numeric value is equal to the position of the

element in the sequence. This rather perverse example illustrates that specifying a numeric predicate gives no guarantee that at most one node will be selected.

An
AxisStep
can contain a sequence of zero or more predicates. Specifying two separate predicates is not the same thing as combining the two predicates into one with an
and
operator. The reason is that the context for the second predicate is different from the context for the first. Specifically, in the second predicate, the context position (the value of the
position()
function) and the context size (the value of the
last()
function) consider only those nodes that successfully passed through the previous predicate. What this means in practice is shown in the examples below:

Expression
Description
book[author = “P. D. James”][1]
The first book that was written by P. D. James.
book[1][author = “P. D. James”]
The first book, provided that it was written by P. D. James.
book[position() = 1 and author = “P. D. James”]
The first book, provided that it was written by P. D. James. This is the same as the previous example, because in that example the second predicate is not dependant on the context position.

Abbreviated Axis Steps

Logically, an axis step has three parts, which we have examined in the previous sections: the axis, the node test, and the predicates. However, the most commonly used axis steps can be written in an abbreviated notation, and in this section we will look at these abbreviations.

In XPath 1.0, the expression
.
was considered to be an abbreviation for the step
self::node()
. In XPath 2.0 this is no longer the case, because
.
can also be used when the context item is an atomic value rather than a node. For this reason,
.
is now classified as a primary expression in its own right, and was therefore covered with the other kinds of primary expression in Chapter 7.

Other books

Heart's a Mess by Scott, Kylie
Close to Famous by Joan Bauer
Summer Rose by Elizabeth Sinclair
The Mandie Collection by Lois Gladys Leppard
Castle Murders by John Dechancie
She's Got It Bad by Sarah Mayberry
Welcome to Dog Beach by Lisa Greenwald
Shepherd One by Rick Jones