Conditional expressions

XPath 2.0 introduced a conditional expression of the form if ( E1 ) then E2 else E3. For example, if (@discount) then @discount else 0 returns the value of the discount attribute if it is present, or zero otherwise.

It's also worth noting the coding pattern (@price, 5)[1] which returns the value of @price if it exists, or 5 otherwise.

Braced conditionals

XPath 4.0 introduces an alternate syntax for conditionals, known as a braced expression. For example, if (condition) then thenExpr else elseExpr may now be written as if condition "{" thenExpr "}" ("else" "{" elseExpr "}")?. The semantics are identical, but the else part is optional, and defaults to an empty sequence.

For example, in XQuery, if ($break) {<br/>} outputs a br element if $break is true, and does nothing if it is false.