Logical (and/or) Expressions
Logical expressions combine Boolean values using the operators and
and or
. They are most often used in conditional (if-then-else
) expressions, where
clauses of FLWORs and path expression predicates. However, they can be used anywhere a Boolean value is expected.
For example, when used in a conditional expression:
if ($isDiscounted and
$discount > 10) then 10 else $discount
an and
expression returns true if both of its operands are true. An or
expression evaluates to true if one or both of its operands is true.
As with conditional test expressions, the effective Boolean value of each of the operands is evaluated. This means that if the operand expression evaluates to a Boolean false
value, the number 0 or NaN
, a zero-length string, or the empty sequence, it is considered false; otherwise, it is generally considered true. For example:
$order/item and $numItems
returns true
if there is at least one item
child of $order
, and $numItems
(assuming it is numberic) is not equal to 0 or NaN
(i.e., not a number).
Evaluation Order of Logical Expressions
The logical operators have lower precedence than comparison operators do, so you can use:
if ($x<
12and
$y>
15) then ...
without parenthesizing the two comparison expressions.
You can also chain multiple and
and or
expressions together. The and
operator takes precedence over the or
operator. Therefore:
true( )and
true( )or
false( )and
false( )
is the same as:
(
true( )and
true( ))
or (
false( )and
false( ))
and evaluates ...
Get XQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.