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

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

The detailed rules for establishing the effective boolean value may appear somewhat arbitrary. They were defined this way in large measure for backward compatibility with XPath 1.0, which allowed sequences of nodes but did not allow sequences of strings, booleans, or numbers. The rules will probably come naturally if you are familiar with weakly typed languages such as Perl or Python, but there are a few traps to beware of. For example, if you convert the boolean value
false
to a string, you get the string
“false”
, but the effective boolean value of this string is
true
.

The
boolean()
function does not always return the same result as the
xs:boolean()
constructor.
xs:boolean()
(like
cast
as
xs:boolean
) follows the rules in XML Schema that define the lexical representations of the
xs:boolean
type. This treats the strings
“1”
and
“true”
as
true
, and
“0”
and
“false”
as
false
; anything else is an error.

XSLT Examples

The following example prints a message if the source document contains a


element and no

, or if it contains a

and no

.


   Document must contain headers and footers,

                                  or neither


The conversion of the two node sequences
//header
(true if there are any


elements in the document) and
//footer
(true if there are any

elements) needs to be explicit here, because we want to do a boolean comparison, not a comparison of two node sequences.

The following example sets a variable to the
xs:boolean
value
true
or
false
, depending on whether the document contains footnotes. In this case the explicit conversion is probably not necessary, since it could be done later when the variable is used, but it is probably more efficient to retain only an
xs:boolean
value in the variable rather than retaining the full set of footnote nodes. An intelligent XSLT processor will recognize that the expression
//footnote
occurs in a context where a boolean is required, and scan the document only until the first footnote is found, rather than retrieving all of them. (In this example, however, using the function
exists()
would achieve the same effect.)


See Also

exists()
on page 778

false()
on page 779

true()
on page 899

ceiling

The
ceiling()
function rounds a supplied number up to the nearest whole number. For example, the expression
ceiling(33.9)
returns
34
.

Changes in 2.0

The function has been generalized to work on all numeric types.

Signature

Argument
Type
Meaning
value
Numeric
The supplied value.
Result
Numeric
The result of rounding
$value
up to the next highest integer. The result has the same primitive type as the supplied value
.

Effect

If the number is an
xs:integer
, or is equal to an
xs:integer
, then it is returned unchanged.

Otherwise, it is rounded up to the next highest whole number. If the supplied value is an
xs:decimal
, the result will be an
xs:decimal
, if it is an
xs:double
, the result will be an
xs:double
, and if it is an
xs:float
, the result will be an
xs:float
.

The
xs:double
and
xs:float
types in XPath support special values such as infinity, negative zero and NaN (not-a-number), which are described on page 199 in Chapter 5. If the argument is NaN, the result will be NaN. Similarly, when the argument is positive or negative infinity, the function will return the value of the argument unchanged.

Other books

Sweet Thursday by Mari Carr
The Bag Lady Papers by Alexandra Penney
Pick 'n' Mix by Jean Ure
The Swan and the Jackal by J. A. Redmerski
Double Trouble by Steve Elliott
Lucy Muir by Highland Rivalry