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

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

that are


elements.

The second predicate is now applied to each node in this sequence of


elements. In each case the predicate
[last()]
returns the same value: a number indicating how many


elements there are in the sequence. As this is a numeric predicate, a node passes the test when
[position() = last()]
, that is, when the position of the node in the sequence (taken in its original order) is equal to the number of nodes in the sequence. So the meaning of
$headings[self::h1][last()]
is “the last


element in the sequence
$headings
.”

Note that this isn't the same as
$headings[last()][self::h1]
, which means “the last item in
$headings
, provided that it is an


element.”

The operation of a
Predicate
in a
FilterExpr
is very similar to the application of a
Predicate
in an
AxisStep
(which we studied in Chapter 9, on page 618), and although they are not directly related in the XPath grammar rules, you can often use predicates without being fully aware of which of these two constructs you are using. For example,
$para[1]
is a
FilterExpr
, while
para[1]
is an
AxisStep
. The main differences to watch out for are, firstly, that in a path expression the predicates apply only to the most recent
Step
(for example, in
book/author[1]
the
[1]
means the first author within each book), and secondly, that in a filter expression the items are always considered in the order of the supplied sequence, whereas in an
AxisStep
they can be in forward or reverse document order, depending on the direction of the axis.

Other books

Fragile Bond by Rhi Etzweiler
The River by Paulsen, Gary
Written in the Scars by Adriana Locke
The No Cry Nap Solution by Elizabeth Pantley
The Professor by Robert Bailey
Doctor Sax by Jack Kerouac
Three Steps to Hell by Mike Holman