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
2.25Mb 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

In the Blink of an Eye by Michael Waltrip
Powerless Revision 1 by Jason Letts
The Unwanted by Brett Battles
Boxcar Children 61 - Growling Bear Mystery by Warner, Gertrude Chandler, Charles Tang
A Night With Consequences by Margaret Mayo
Just Remember to Breathe by Charles Sheehan-Miles
Waiting For You by Ava Claire
Loki's Wolves by K. L. Armstrong, M. A. Marr
Hell's Kitchen by Callie Hart, Lili St. Germain