Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
In effect, the predicate operator
[
]
has higher precedence (it binds more tightly) than the path operator
/
.
Another distinction between the two cases is that in the case of a
FilterExpr
, the items are always considered in their original order when evaluating the predicate. In the case of an
AxisStep
, the nodes are considered in the order of the relevant axis. This is explained in more detail below.
A predicate may be either a boolean expression or a numeric expression. These are not distinguishable syntactically; for example, the predicate
[$p]
could be either. The distinction is only made at runtime. (That's the official rule, anyway. If an optimizer can work out in advance whether the value is numeric or boolean, then it will. It's a good idea to declare the types of your variables and parameters, which will make the optimizer's job easier.)
The following table shows some examples of boolean predicates:
Expression | Description |
section[@title = ‘Introduction’] | Here the predicate is a conventional boolean expression. This example selects every child title attribute with the value Introduction . |
section[title] | The predicate is true if the relevant section has at least one child |
title[substring-before(.,‘:’)] | The PredicateExpr evaluates to true if the string-value of the title has one or more characters before its first colon: that is, if the substring-before() function returns a nonempty string. |
book[not(author = preceding-sibling::author)] | The PredicateExpr here is true if the author of the book is not the same as the author of some preceding book within the same parent element. The effect of this expression is to select the first book by each author. |
If the value of the predicate is a number (that is, if its type label is
xs:decimal
,
xs:integer
,
xs:float
, or
xs:double
, or some subtype of these), it is treated as a numeric predicate. If it is of any other type, it is converted to an
xs:boolean
value using the effective boolean value rules described in Chapter 7 (these are the same as the rules for the
boolean()
function). So for example, the predicate
[@sequence-number]
is true if the context node has a
sequence-number
attribute, and is false otherwise. The actual numeric value of the attribute
sequence-number
is immaterial: the value of
@sequence-number
is a sequence of nodes, so it is treated as
[boolean(@sequence-number)]
. If you want to use the sequence number attribute as a numeric predicate, write
[number(@sequence-number)]
. Or, if you prefer brevity, write
[+@sequence-number]
.