Logical expressions
Logical expressions use the operators and and or to compute a Boolean value (true or false).
The following table describes these operators and lists them in order of operator precedence from highest to lowest.
Operator | Purpose |
---|---|
and | Returns true if both expressions are true. |
or | Returns true if one or both expressions are true. |
The result of a logical expression is either a Boolean
value (true or false) or an error. When a logical expression is evaluated,
the effective Boolean value (EBV) of each operand is determined. The
operator is then applied to the EBVs of the operands, and the result
is either a boolean value or an error. If the EBV of an operand is
an error, then the logical expression might result in an error. The
following table shows the results that are returned by a logical expression
based on the EBVs of its operands.
EBV of operand 1 | Operator | EBV of operand 2 | Result |
---|---|---|---|
true | and | true | true |
true | and | false | false |
false | and | true | false |
false | and | false | false |
true | and | error | error |
error | and | true | error |
false | and | error | false or error |
error | and | false | false or error |
error | and | error | error |
true | or | true | true |
false | or | false | false |
true | or | false | true |
false | or | true | true |
true | or | error | true or error |
error | or | true | true or error |
false | or | error | error |
error | or | false | error |
error | or | error | error |
Tip: In addition to logical expressions, XQuery
provides a function named fn:not that takes a general sequence as
a parameter and returns a Boolean value.
Examples
- The following expressions return true:
1 eq 1 and 2 eq 2 1 eq 1 or 2 eq 3
- The following expression might return either false or an error:
1 eq 2 and 3 idiv 0 = 1
- The following expression might return either true or an error:
1 eq 1 or 3 idiv 0 = 1
- The following expression returns an error:
1 eq 1 and 3 idiv 0 = 1