Otherwise operator

XPath 4.0 introduces 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:

chapter[title='Introduction'] otherwise chapter[1]

returns the chapter(s) entited "Introduction" if such a chapter exists; otherwise it returns the first chapter.

The precedence of the operator as implemented in Saxon 13 matches the current XPath 4.0 draft specification (September 2024), which has changed since Saxon 12. The precedence is greater than comparison operators such as =, but less than arithmetic operators such as +. So $a = $b + $c otherwise $d means $a = (($b + $c) otherwise $d). Use parentheses to make your intended meaning clear and to avoid any compatibility problems.