XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / XPath – filtering sequences / Predicates – filter conditions for location steps

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 2.)

In XPath, the required filter condition is defined using a predicate, usually translated into German, somewhat awkwardly, as ‘Prädikat’ (Note: ‘Filter condition’ or ‘predicate filter’ would be more appropriate. I prefer the shorter English term as a technical term, rather than out of a fondness for anglicisms.) A predicate is enclosed in square brackets and simply appended to the axis specifier and node test of a step:

achsenbezeichner::knotentest[predicate]

A predicate may contain any XPath expression containing identifiers, operators and function calls. A step may have several consecutive predicates:

achsenbezeichner::knotentest[predicate1][predicate2]

Think of this as a series of sieves through which the result set of the location step is filtered. Remember that the resulting set can depend on the order of the predicates. This becomes clearer if you imagine a condition that removes every second item from a sequence, and another condition that filters the remainder by comparing it with a threshold.

If a path expression consists of several steps, each step may have any number of predicates:

step1[predicate]/step2[predicate]/step3[predicate]

Each predicate expression is evaluated as a Boolean value in the context of a candidate node. If the value is true, the tested node is added to, or retained in, the step’s result set; otherwise it is discarded. The sequence remaining after the final step is the result sequence of the path expression.

This lets you control the composition of a sequence in detail, as the following simple examples will show.

   

back to top