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

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

xs:boolean

This is the simplest type defined in XML Schema. It has two values in the value space, referred to as
true
and
false
, and each of these has two permitted lexical representations:
1
and
true
,
0
and
false
.

Although it's so simple, there are some interesting quirks in the way XML Schema and XPath handle this type.

  • As far as XML Schema is concerned, the
    xs:boolean
    type has no ordering. But in XPath, there is an ordering:
    false
    is considered to be less than
    true
    . XPath 2.0 has taken this position largely for backward compatibility with XPath 1.0, and also because it can actually be useful; for example, a stylesheet might use the expression
    age < 18
    as a sort key, which will output the adults first, then the children.
  • There are two ways of converting a string to a boolean. An XML Schema processor interprets
    1
    and
    true
    as true,
    0
    and
    false
    as false. This behavior also occurs when you use the
    xs:boolean()
    constructor (described in Chapter 11). But if you use the
    boolean()
    function (or
    fn:boolean()
    if you want to write it with a namespace prefix), as described in Chapter 13, then a zero-length string translates to
    false
    , and everything else to
    true
    . This is also the result you get if you do an implicit conversion of a string to a boolean by using a string in a context such as
    if (S) then A else B
    , where S is a string.

    Again, the difference is partly historic: the XPath 1.0 rules were invented before XML Schema came along. But the convention of equating a zero-length string to
    false
    also has a long history in weakly typed programming languages, and is very convenient in some recursive algorithms that need to terminate when the argument is a zero-length string.

Other books

Swim Back to Me by Ann Packer
One Thousand Years by Randolph Beck
Alentejo Blue by Monica Ali
Avenging Angel by Cynthia Eden
Embrace the Wind by Caris Roane
A Mile High by Bethany-Kris
The Glass Castle by Priebe, Trisha; Jenkins, Jerry B.;