Predicates

Predicates occur after the node test in square brackets. A wide range of expressions are possible.

nodetest[1]

Matches the first node

Most node tests return nodes in document order, only the tests which select ancestor or preceding elements return nodes in reverse document order. The practical result of this is that the "first" node is almost always the one closest to the context node, although parenthesis can change the effective order.

nodetest[position()=last()]

Matches the last node

nodetest[position() mod 2 = 0]

Matches even nodes

element[@id='foo']

Matches the element(s) with id attributes whos value is "foo"

element[not(@id)]

Matches elements that don't have an id attribute

author[firstname="Norman"]

Match <author> elements that have <firstname> children with the content "Norman".

author[normalize-space(firstname)="Norman"]

Match "Norman" without regard to leading and trailing space.

Predicate expressions can be more-or-less arbitrarily complex.