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.68Mb 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

AKLESH (Under Strange Skies) by Pettit, Samuel Jarius
And the Bride Wore Plaid by Karen Hawkins
My Little Secret by Anna J.
Liars by Glenn Beck
Guarding a Notorious Lady by Olivia Parker
Seduction (Club Destiny) by Edwards, Nicole
By Honor Betray'd: Mageworlds #3 by Doyle, Debra, Macdonald, James D.