Otherwise operator
XPath 4.0 proposes an additional operator: otherwise
. The expression A otherwise B
returns the value of A
unless it is an empty sequence, in which case it returns the value of B
.
For example, the expression:
returns the chapter(s) entited "Introduction" if such a chapter exists; otherwise it returns the first chapter.
As defined in the current XPath 4.0 draft (September 2024), arithmetic operators such as +
bind more tightly than otherwise
, so $a + $b otherwise $c
means ($a + $b) otherwise $c
. However, Saxon 12 implements an earlier draft of the
specification in which it means $a + ($b otherwise $c)
. Use parentheses to make your
intended meaning clear and to avoid compatibility problems in the future.