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

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

Casting to an xs:integer

Generally, casting to a derived type fails if the facets of the derived type are not satisfied. The type
xs:integer
is derived by restriction from
xs:decimal
, with a facet indicating that there must not be a fractional part. Normally, this would mean that casting 12.3 to an
xs:integer
would fail.

However, there is a special rule for casting numeric values to integers, or to subtypes derived from
xs:integer
. The value is first cast to the primitive type,
xs:decimal
, in the usual way, and then the value is truncated toward zero. This means that
xs:integer(10
div
3)
is
3
, and
xs:integer(3.5
-
8.7)
is
-5
.

This special rule does not apply when casting from a string to an integer. In this case
xs:integer
is treated like any other derived type, which means that a cast such as
xs:integer(“2.5”)
will fail.

Casting to xs:yearMonthDuration and xs:dayTimeDuration

When an
xs:duration
is cast to an
xs:yearMonthDuration
or to an
xs:dayTimeDuration
, the components of the supplied value that are not applicable to the target type are discarded. This ensures that the cast will always succeed. For example, if the supplied value is
xs:duration(‘P1Y1D’)
(one year and a day, a duration found in insurance policies and fairy tales), casting to
xs:yearMonthDuration
produces
P1Y
, while casting to
xs:dayTimeDuration
produces
P1D
.

This rule applies only when the supplied value is an instance of
xs:duration
or one of its subtypes (either one of the built-in subtypes, or a user-defined subtype). It does not apply, for example, when casting from
xs:string
. Furthermore, it applies only to these built-in duration subtypes, and any user-defined types derived from them. It does not apply to a user-defined type derived directly from
xs:duration
, even one whose definition is identical to that of the built-in type.

Sequence Type Descriptors

The operators described in the previous section work only on atomic types, which can always be referenced by a simple
QName
(unless they are anonymous types, in which case they can't be referenced at all). Later in this chapter, we will be defining two important operators
instance
of
and
treat
as
. These can be applied to any sequence type (that is, any type in the XPath type system). Sequence types cannot always be represented by a simple name; instead, XPath defines a syntactic construct for describing these types. The production rule for this is called
SequenceType
. I find it useful to have a name for the actual description of a type written according to this syntax, so I've coined the name
sequence type descriptor
for this.

Sequence type descriptors are used in XPath itself only in expressions involving these two operators
instance
of
and
treat
as
. However, they are used much more widely in XSLT, notably to declare the types of variables, functions, and parameters in the
as
attribute of

,

,

,

, and other similar places. They also arise in XQuery.

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

Other books

An Angel in the Mail by Callie Hutton
Love Me: The Complete Series by Wall, Shelley K.
Bite Me by Jenika Snow
Leadville by James D. Best
Un hombre que promete by Adele Ashworth
Lady Pirate by Lynsay Sands
Soul Trade by Caitlin Kittredge
Destroy Me by Laura Bailey
Eat, Drink and Be Wary by Tamar Myers