Short-circuit boolean operators
Saxon provides two additional boolean operators: orElse
and andAlso
.
These have the same effect as or
and and
, except that there is a guarantee
that the second operand will not be evaluated unnecessarily. This makes it possible to ensure that
evaluating the second operand does not produce an error. For example, the expression:
//chapter[pages=0 orElse length div pages]
will never fail with a divide-by-zero error, because the division will never be executed if the
value of pages
is zero.
When the ordinary or
and and
operators are used, the W3C specification
gives XPath processors license to re-arrange the order of execution, and Saxon will occasionally do so.
Using these operators is therefore advised when the first condition is a guard to prevent the second condition
failing.
The same effect can be achieved without relying on extensions, by writing
A orElse B
as if (A) then true() else boolean(B)
, and rewriting
X andAlso Y
as if (X) then boolean(Y) else false()
.