XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / XPath – Filtering sequences / Combining conditions logically with AND and OR

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

A predicate can contain several logically combined conditions that must hold simultaneously, AND, or alternatively, OR. The two XPath expressions, each of which must produce a Boolean value, are joined inside the predicate’s square brackets by token and or or. Since the precedence of logical operators is lower than that of other operators, such as arithmetic or comparison operators, the combined expressions need no parentheses.

Union – inclusive OR

To see authors 'Hafner, Katie' and 'Hass, Hans', construct a filter predicate with an OR relation that lets both authors pass:

buch[buchautor='Hafner, Katie' or buchautor='Hass, Hans']

This selects books with either author, individually or together. The conditions need not be similar; for example:

buch[buchautor='Panter, Peter' or @kat='reisen']

This is the union of all books by Peter Panter and all books in category “Reisen”. Book elements satisfying both conditions occur only once in the result sequence, although nodes may in general occur more than once in sequences.

Intersection – exclusive AND

Use an AND relation when both conditions must hold simultaneously. The result essentially matches two independent predicates: only nodes satisfying both conditions are included.

[buchautor='Hafner, Katie' and buchautor='Lyon, Matthew']

This requires a book to have at least two <buchautor> elements containing the two tested values.

Operators in predicates

Here is a brief list of operators usable in predicates. Their order from top to bottom corresponds to the operator precedence mentioned above.

Table: operators in predicates and their precedence.

Test/task

Operator

Unary minus, sign

-

Multiplicative operators

*, div, idiv, mod

Additive operators

+, -

Comparison operators (<, > replace with &gt; and &lt;)

=, !=, <, >, <=, >=, eq, ne, lt, gt, le, ge, is, isnot

Combined condition

and

Alternative condition

or

   

back to top