XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (313 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
5.71Mb size Format: txt, pdf, ePub

Note that both branches (the
then
and the
else
) must be present. It's quite common to write
else
()
to return nothing (an empty sequence) when the condition is
false
, but you have to write this explicitly.

The expression used as the condition to be tested (inside the parentheses) is evaluated to give its
effective boolean value
. Unusually, XPath 2.0 doesn't apply strict type checking to this expression, rather it defines a set of rules allowing a wide range of values to be converted to the
xs:boolean
values
true
or
false
. The rules are the same as for the
boolean()
function described on page 721, in summary:

  • If the condition is an empty sequence, its effective boolean value is
    false
    .
  • If the condition is a sequence whose first item is a node, its effective boolean value is
    true
    .
  • If the condition is a singleton
    xs:boolean
    value, the result is this value.
  • If the condition is a singleton numeric value, the result is false if the argument is NaN or zero; otherwise, it is true.
  • If the condition is a singleton instance of
    xs:string
    ,
    xs:anyURI
    , or
    xs:untypedAtomic
    , the result is false if the string is zero length; otherwise, it is true.
  • In all other cases, an error is reported.

There is no atomization applied to any nodes in the sequence. This means that if the value includes one or more nodes, the result is
true
, regardless of the contents of the node. Even if
@married
is an attribute whose typed value is the
xs:boolean
value false, the result of the expression
if (@married) then
“yes“ else “no”
is the string
yes
. If you want to test the contents of the node, rather than testing for its existence, use the
data()
function to atomize it explicitly, or write the test in the form
if
(@married
=
true())
then
..
.

Other books

This Old Murder by Valerie Wolzien
Heritage of Flight by Susan Shwartz
The Last Empire by Gore Vidal
Life's Next Chapter by Goodman, Sarah
The Saint Goes On by Leslie Charteris
Gambler's Woman by Jayne Ann Krentz
Against the Wind by Kat Martin
How to Wash a Cat by Rebecca M. Hale
Gift Wrapped by Peter Turnbull