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

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

if (every $s in $N/@size satisfies $s eq 0) …

XSLT Examples

The following test succeeds if the context node has no children:


The following test succeeds if the context node has no parent (that is, if it is a root node):


The following

statement processes all the child elements of the context node except the

elements:


The following test succeeds if the string-value of the context node is zero-length (assuming that the context item is a node):


The following test succeeds if the
name
attribute of the context node is absent or is a zero-length string:


The following test succeeds if the
name
attribute of the first node in node-set
$ns
is different from the
name
attribute of each subsequent node in the node-set (we assume that this attribute is present on all nodes in the node-set):


See Also

boolean()
on page 721

false()
on page 779

true()
on page 899

number

The
number()
function converts its argument to a value of type
xs:double
.

For example, the expression
number(‘
-17.3’)
returns the
xs:double
value –17.3e0.

Changes in 2.0

A leading
+
sign is allowed in the number, and exponential notation is permitted, to align the rules with XML Schema.

Signature

Argument
Type
Meaning
value
(optional)
item()?
The value to be converted. If the argument is omitted, the context item is used. If an empty sequence is supplied, the result is NaN.
Result
xs:double
A double-precision floating-point number: the result of converting the given
value
. If the argument cannot be converted to a number, the function returns NaN (not-a-number)
.

Effect

The conversion rules used are the same as the rules for casting to an
xs:double
(and therefore, the same as the
xs:double()
constructor function), with the exception that if the value is not convertible to a number, the result is NaN (not-a-number) rather than an error.

If the value supplied is a node, then the node is first atomized in the usual way.

The only atomic types that can be converted to a number are booleans, strings, and other numbers. The conversion is as follows:

Supplied type
Conversion rules
xs:boolean
false
becomes zero;
true
becomes one.
xs:string
The rules are the same as the rules for writing an
xs:double
value in XML Schema.
xs:integer
,
xs:decimal
,
xs:float
The result is the same as converting the value to a string, and then converting the resulting string back to an
xs:double
.

Examples

Expression
Result
number(12.3)
xs:double 12.3e0
, displayed as
“12.3”
number(“12.3”)
xs:double 12.3e0
, displayed as
“12.3”
number(true())
xs:double 1.0e0
, displayed as
“1”
number(“xyz”)
xs:double NaN
number(“”)
xs:double NaN

Usage

In XPath 1.0, conversion to a number was generally implicit so it was rarely necessary to use the
number()
function explicitly. This remains the case if XPath 1.0 backward-compatibility mode is used. When this mode is not enabled, however, type errors will be reported when strings or booleans are supplied in contexts where a number is expected; for example, as operands to numeric operators such as
+
. You still get implicit conversion when you supply an untyped node as the operand (for example,
@code+1
is okay), but not when the value is explicitly typed. For example, if the
date-of-birth
attribute is an untyped string in the format of an ISO 8601 date, the following is an error under XPath 2.0 rules:

substring(@date-of-birth, 1, 4) < 1970

This is because
substring()
returns a string, and you cannot compare a string to a number. Instead, write:

number(substring(@date-of-birth, 1, 4)) < 1970

There is one important situation where conversion needs to be explicit: this is in a
predicate
. The meaning of a predicate depends on the type of the value, in particular, a numeric predicate is interpreted as a comparison with the context position. If the value is not numeric, it is converted to a boolean.

So for example, if a value held in an attribute or in a temporary tree is to be used as a numeric predicate, you should convert it explicitly to a number, thus:

$sales-figures[number(@month)]

To test whether a value (for example, in an attribute) is numeric, use
number()
to convert it to a number and test the result against NaN (not-a-number). The most direct way to do this is:

if (string(number(@value))=‘NaN’) then …

Alternatively, use the
castable as
operator described in on page 655 in Chapter 11.

Other books

Otherworld Challenger by Jane Godman
The Badger's Revenge by Larry D. Sweazy
Quicker Than the Eye by Ray Bradbury
The Stranger Beside You by William Casey Moreton
MasterofVelvet by Kirstie Abbot
Finding Abigail by Carrie Ann Ryan
Fire on the Island by J. K. Hogan