where clauses

A where clause in a FLWOR expression filters the tuples of variable bindings that are generated by the for and let clauses.

The where clause specifies a condition that is applied to each tuple of variable bindings. If the condition is true (that is, if the expression results in an effective Boolean value of true), then the tuple is retained, and its bindings are used when the return clause executes. Otherwise, the tuple is discarded.

In the following example, the for clause binds the variables $x and $y to sequences of numeric values:
for $x in (1.5, 2.6, 1.9), $y in (.5, 1.6, 1.7)
where ((fn:floor($x) eq 1) and (fn:floor($y) eq 1))
return <output>{$x, $y}</output>
When the for clause is evaluated, nine tuples of variable bindings are created:
($x = 1.5, $y = .5)
($x = 2.6, $y = .5)
($x = 1.9, $y = .5)
($x = 1.5, $y = 1.6)
($x = 2.6, $y = 1.6)
($x = 1.9, $y = 1.6)
($x = 1.5, $y = 1.7)
($x = 2.6, $y = 1.7)
($x = 1.9, $y = 1.7)
The where clause filters these tuples, and the following tuples are retained:
($x = 1.5, $y = 1.6)
($x = 1.9, $y = 1.6)
($x = 1.5, $y = 1.7)
($x = 1.9, $y = 1.7)
The return clause executes once for each remaining tuple, and the expression results in the following output:
<output>1.5 1.6</output>
<output>1.9 1.6</output>
<output>1.5 1.7</output>
<output>1.9 1.7</output>

Because the expression in this example does not include an order by clause, the order of the output elements is non-deterministic.