XML technologies / XSLT / XSLT and XPath functions / XPath 2.0 casting functions
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5.)
XPath 2.0 supports conversion with constructor functions taking an XPath expression and returning a typed value, or with a casting function (casting function) specified after operator cast as.
Behavior is identical, with slightly different notation:
Constructor: xs:date("2003-06-06")
Casting function: cast as xs:date ("2003-06-06")
For a fixed literal not computed at runtime, a failed cast can be detected as a static error while reading the stylesheet. Otherwise conversion is checked at runtime and failure is a runtime error.
Casting between primitive types
Primitive types and derived data types are also called atomic types (atomic types) because values are indivisible and cannot contain other values. This includes XPath/XQuery strings, since XPath has no separate character type (character values), as some programming languages do.
XPath 2.0 primitive types include:
untypedAtomic (from XPath), anySimpleType, string, float, double, decimal, duration, yearMonthDuration, dayTimeDuration, dateTime, time, date, gYearMonth, gYear, gMonthDay, gDay, gMonth, boolean, base64Binary, hexBinary, anyURI, QName and NOTATION.
Casting is generally possible if the value has lexical compatibility, matching the target type’s required form.
Other conversions are either always possible or always impossible.
Source type: untypedAtomic
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | no |
Table: Casting untypedAtomic to other target types.
Note: Casting to every unlisted type is possible with lexical compatibility.
Source type: anySimpleType
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
anySimpleType | yes | NOTATION | no |
string | yes |
Table: Casting anySimpleType to other target types.
Note: Casting to every unlisted type is possible with lexical compatibility.
Warning – List-valued anySimpleType
Type xs:anySimpleType can have list instances, which cannot be cast to xs:untypedAtomic.
Source type: string
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | NOTATION | no |
Table: Casting string to other target types.
Note: Casting to every unlisted type is possible with lexical compatibility.
Source type: float
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | double | yes |
anySimpleType | yes | decimal | possibly |
string | yes | boolean | yes |
yes |
Table: Casting float to other target types.
Note: Casting to all unlisted types is impossible.
Source type: double
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | double | yes |
anySimpleType | yes | decimal | possibly |
string | yes | boolean | yes |
float | yes |
Table: Casting double to other target types.
Note: Casting to all unlisted types is impossible.
Source type: decimal
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | double | yes |
anySimpleType | yes | decimal | yes |
string | yes | boolean | yes |
float | yes |
Table: Casting decimal to other target types.
Note: Casting to all unlisted types is impossible.
Source type: duration
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | duration | yes |
anySimpleType | yes | yearMonthDuration | yes |
string | yes | dayTimeDuration | yes |
Table: Casting duration to other target types.
Note: Casting to all unlisted types is impossible.
Source type: yearMonthDuration
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | duration | yes |
anySimpleType | yes | yearMonthDuration | yes |
string | yes |
Table: Casting yearMonthDuration to other target types.
Note: Casting to all unlisted types is impossible.
Source type: dayTimeDuration
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | duration | yes |
anySimpleType | yes | dayTimeDuration | yes |
string | yes |
Table: Casting dayTimeDuration to other target types.
Note: Casting to all unlisted types is impossible.
Source type: dateTime
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | gYearMonth | yes |
anySimpleType | yes | gYear | yes |
string | yes | gMonthDay | yes |
dateTime | yes | gDay | yes |
time | yes | gMonth | yes |
date | yes |
Table: Casting dateTime to other target types.
Note: Casting to all unlisted types is impossible.
Source type: time
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | dateTime | yes |
anySimpleType | yes | time | yes |
string | yes |
Table: Casting time to other target types.
Note: Casting to all unlisted types is impossible.
Source type: date
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | gYearMonth | yes |
anySimpleType | yes | gYear | yes |
string | yes | gMonthDay | yes |
dateTime | yes | gDay | yes |
date | yes | gMonth | yes |
Table: Casting date to other target types.
Note: Casting to all unlisted types is impossible.
Source type: gYearMonth
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | gYearMonth | yes |
Table: Casting gYearMonth to other target types.
Note: Casting to all unlisted types is impossible.
Source type: gYear
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | gYear | yes |
Table: Casting gYear to other target types.
Note: Casting to all unlisted types is impossible.
Source type: gMonthDay
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | gMonthDay | yes |
Table: Casting gMonthDay to other target types.
Note: Casting to all unlisted types is impossible.
Source type: gDay
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | gDay | yes |
Table: Casting gDay to other target types.
Note: Casting to all unlisted types is impossible.
Source type: gMonth
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | gMonth | yes |
Table: Casting gMonth to other target types.
Note: Casting to all unlisted types is impossible.
Source type: boolean
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | double | yes |
anySimpleType | yes | decimal | yes |
string | yes | boolean | yes |
float | yes |
Table: Casting boolean to other target types.
Note: Casting to all unlisted types is impossible.
Source type: base64Binary
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | boolean | possibly |
anySimpleType | yes | base64Binary | yes |
string | yes |
Table: Casting base64Binary to other target types.
Note: Casting to all unlisted types is impossible.
Source type: hexBinary
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | boolean | possibly |
anySimpleType | yes | hexBinary | yes |
string | yes |
Table: Casting hexBinary to other target types.
Note: Casting to all unlisted types is impossible.
Source type: anyURI
Target type | Conversion | Target type | Conversion |
untypedAtomic | yes | string | yes |
anySimpleType | yes | anyURI | yes |
Table: Casting anyURI to other target types.
Note: Casting to all unlisted types is impossible.
Source type: QName
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | yes |
Table: Casting QName to other target types.
Note: Casting to all unlisted types is impossible.
Source type: NOTATION
Target type | Conversion | Target type | Conversion |
|---|---|---|---|
untypedAtomic | yes | string | yes |
anySimpleType | yes | NOTATION | possibly |
Table: Casting NOTATION to other target types.
Note: Casting to all unlisted types is impossible.
Note – Casting to xs:NOTATION is problematic
Casting any source type to xs:NOTATION is forbidden except from xs:NOTATION because notation validity depends on context and cannot be predetermined.
Casting derived types to base types
A type can always be cast to the type from which it derives by restriction (restriction), because its value space (value space) must be a subset of the base type’s value space.
For example, type xs:unsignedShort always casts to xs:unsignedInt, xs:unsignedLong, xs:unsignedInteger, xs:integer or xs:decimal. The value is unchanged, with no information loss; only its datatype changes.
Casting within a type-hierarchy branch
Two types derived directly or indirectly by restriction from one base can be cast between each other. The value must satisfy the target’s restrictive facets because their value spaces differ. This includes casting a type to one derived from itself.
For example, a value of xs:byte can become xs:unsignedShort only if nonnegative, as required by the target.
Casting across type-hierarchy branches
Casting between types derived from different primitives crosses the type hierarchy (casting across the type hierarchy).
It has three logical stages, each of which can fail:
Upcasting: Cast the source to its base type, moving up the hierarchy; generally possible.
Crosscasting: Cast that base to the target’s base using primitive-type conversion rules.
Downcasting: Cast down from the target base to the target, respecting restrictive facets.
If all three stages work, the cast succeeds.
From xs:string and xs:anySimpleType
An instance of xs:string, xs:anySimpleType or xs:untypedAtomic is treated as a string and checked against target lexical requirements. String types can cast to restriction-derived types except xs:NOTATION. A separate case is casting to xs:QName.
To xs:string, xs:anySimpleType and xs:untypedAtomic
Primitive types such as xs:string, xs:anySimpleType and xs:untypedAtomic can be reached from all other primitives.
All three cases first cast to xs:string. To obtain xs:anySimpleType or xs:untypedAtomic, only the type assignment then changes. Values already of source type xs:string need no further value adjustment.
From xs:anyURI
Casting datatype xs:anyURI to xs:string may apply URI escaping. Spaces in the URI string, for example, become hexadecimal sequence %20.
From xs:QName
Casting an xs:QName to string resolves its associated namespace URI to a prefix. If several in-scope prefixes bind the same namespace, the implementation chooses any one; the resulting prefix may differ from the original.
The string has QName lexical form: prefix plus local name, retaining the original local name.
From xs:datetime, xs:date or xs:time
Types xs:datetime and xs:time can include timezones that string conversion must consider. With xs:datetime, the timezone follows date and time:
UTC, marked by suffix Z:
2003-07-10T10:30:00Z
Eastern Standard Time, five hours behind UTC:
2003-07-10T10:30:00-05:00
Without an explicit timezone, that is with a UTC value, trailing Z is removed before string conversion. Otherwise the timezone difference is incorporated into the value.
To numeric types
Numeric target types include xs:float, xs:double, xs:decimal and xs:integer. Nonnumeric input must have the target’s lexical form.
From numeric types
Numeric conversions work within representable ranges. Otherwise a value may become INF or -INF or, with underflow below available precision, approximate to 0. Casting to xs:integer discards fractional digits without rounding.
From string types
Strings must match the numeric lexical form, with digits and permitted decimal separator, or be convertible, perhaps using function fn:upper-case(), into “INF”, “-INF” or “NaN”.
From boolean values
Boolean true converts to 1.0 and false to 0.0. For target xs:integer, these are 1 and 0 respectively.
To duration types
Casting to xs:duration, xs:yearMonthDuration or xs:dayTimeDuration is possible from xs:untypedAtomic, xs:anySimpleType or xs:string with valid content.
Input type xs:duration always converts to xs:yearMonthDuration or xs:dayTimeDuration, losing either year/month or day/hour/minute/second information.
Beware of information loss
Beware that casting xs:duration to xs:yearMonthDuration or xs:dayTimeDuration loses information. Cast to both types and continue with both results to avoid this.
To date and time types
Casting to xs:dateTime, xs:time, xs:date, xs:gYearMonth, xs:gYear, xs:gMonthDay, xs:gDay or xs:gMonth generally requires matching lexical form. A predefined string operation may append timezone information.
Depending on source type, conversion formally follows date/time extraction functions.
To xs:boolean
Any primitive type can become xs:boolean. Numeric conversions are direct: input 0, +0, -0 and NaN yield false and all others yield true.
Other sources first form a token: “true” or “1” yields true, while “false” or “0” yields false. Other tokens violate the lexical rules of xs:boolean and cause an error.
To xs:base64Binary and xs:hexBinary
Casting to xs:base64Binary and xs:hexBinary is supported only in special cases from xs:string or its subtypes.
To xs:anyURI
Casting to type xs:anyURI is possible from xs:string or from types derived from xs:string, xs:anySimpleType or xs:untypedAtomic. Input must lexically match xs:anyURI or conversion fails with error “Invalid lexical value”.
To xs:QName
Casting to type xs:QName is possible from xs:string or from types derived from xs:string, which are also treated as xs:string.
The new QName’s local name comes from input. If a prefix exists, the URI is selected from static-context namespaces. Without a prefix it uses the default namespace or no namespace if none exists.
To xs:NOTATION
Casting any source type to xs:NOTATION is forbidden except possibly from an existing value of xs:NOTATION, since notation validity depends on context and cannot be predetermined.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn