XML technologies / XPath / XPath 2.0: language reference / XPath expressions: operators and keywords / Quantified expressions

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)

Keywords: some, every, in, satisfies

A quantified expression (quantified expression) describes sequence composition or relationships between sequences. Its result is always true or false, making it related to comparison expressions.

Quantification is universal or existential. The respective initial keywords are every or some.

The initial keyword is followed by one or more comma-separated expressions that use in to bind temporary variables to sequences. Finally, keyword satisfies introduces the condition tested.

Existential quantification

Existential quantification means that the tested sequence contains an item satisfying the condition:

some $x in $sequenz       satisfies $bedingung

The variable $x is bound successively to each item of sequence $sequenz.

The test expression $bedingung references the temporary variable. If any binding satisfies the condition, the result is true; if none does, or the sequence is empty, the result is false.

Multiple variables can also be bound to multiple sequences:

some $a in $sequenz_a,      $b in $sequenz_b,      ...,      $n in $sequenz_n      satisfies $bedingung

With multiple bindings, every possible tuple ($a, $b, ..., $n) is tested against a condition referencing variables $a through $n. Tuples formally form the Cartesian product of the bindings: every possible combination containing one item from each set.

For example, suppose sequence A has two items a1 and a2 and sequence B also has two items b1 and b2. The tuples (a1, b1), (a1, b2), (a2, b1), (a2, b2) are formed. The bound variables $a, $b therefore satisfy:

($a, $b) =  (a1, b1) | (a1, b2) | (a2, b1) | (a2, b2)

The expression is true if at least one resulting tuple satisfies the condition.

If no such tuple exists, the result is false. If any participating sequence is empty, no tuples can be formed and the result is also false.

Example 1: one bound variable

some $x in /buecher/buch/autor       satisfies $x = "James Joyce"

This expression returns true if any tested book has author “James Joyce”.

The XPath 1.0 equivalent is:

/buecher/buch/autor = "James Joyce"

Example 2: two bound variables

some $x in (1, 2, 3),      $y in (2, 3, 4)       satisfies $x + $y = 4

This expression returns true because the condition holds for tuples ($x=1, $y=3) or ($x=2, $y=2).

Looking back at XPath 1.0

XPath 1.0 has only one operator usable as a quantifier: equality, which effectively performs existential quantification, as in example 1 above.

Universal quantification

Universal quantification requires every item of a binding, or every tuple for multiple bindings, to satisfy the condition.

Example 1: one bound variable

every $x in /buecher/buch/autor        satisfies $x = "James Joyce"

This expression returns true if every tested book has author “James Joyce”. A single author element with another value makes it return false. No equivalent expression exists in XPath 1.0.

Example 2: two bound variables

every $x in (1, 2, 3),       $y in (2, 3, 4)        satisfies $x + $y = 4

This expression returns false because the condition fails for tuples ($x=1, $y=2) or ($x=2, $y=3) and so on.

Caution: empty sequences
By definition, universal quantification returns true if any variable is bound to an empty sequence: no tuples can be formed for testing. This also applies to a single binding when its sequence is empty, as in example 1. The universal quantifier every therefore behaves oppositely to existential some.

Looking back at XPath 1.0:

XPath 1.0 has no counterpart to universal quantifiers.

   

back to top