Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The sections that follow describe all the rules for converting a supplied value to a target type. We'll start by considering the rules for converting from a primitive type to another primitive type and then go on to consider how derived types are handled.
Converting between Primitive Types
The type conversions described in this section start with an atomic value that is labeled with a primitive type. For these purposes we consider a primitive type to be either one of the primitive types defined in Part 2 of the XML Schema specification, or the XPath-defined atomic type
xs:untypedAtomic
.. Although in XPath the types
xs:integer, xs:dayTimeDuration
and
xs:yearMonthDuration
are sometimes treated as primitive, in this chapter we will treat them as derived types, which is what they actually are.
For reasons of space, few examples of conversions are given in this chapter. However, in the download files for this book there is a stylesheet,
cast.xsl
, which exercises all the conversions between primitive types. It requires a schema-aware XSLT processor.
We couldn't get this stylesheet to run with our version of AltovaXML 2008.
The following table lists for each source type, the permitted destination types. The detailed rules for these conversions are then given in the subsequent sections, which for ease of reference are arranged alphabetically according to the type of the source value for the conversion.
Source Type | Permitted Result Types |
anyURI | anyURI , string , untypedAtomic |
base64Binary | base64Binary , hexBinary , string , untypedAtomic |
boolean | boolean , decimal , double , float , string , untypedAtomic |
date | date , dateTime , gDay , gMonth , gMonthDay , gYear , gYearMonth , string , untypedAtomic |
dateTime | date , dateTime , gDay , gMonth , gMonthDay , gYear , gYearMonth , string , time , untypedAtomic |
decimal | boolean , decimal , double , float , string , untypedAtomic |
double | boolean , decimal , double , float , string , untypedAtomic |
duration | duration , string , untypedAtomic |
float | boolean , decimal , double , float , string , untypedAtomic |
gDay | gDay , string , untypedAtomic |
gMonth | gMonth , string , untypedAtomic |
gMonthDay | gMonthDay , string , untypedAtomic |
gYear | gYear , string , untypedAtomic |
gYearMonth | gYearMonth , string , untypedAtomic |
hexBinary | base64Binary , hexBinary , string , untypedAtomic |
NOTATION | NOTATION , string , untypedAtomic |
QName | QName , string , untypedAtomic |
string | anyURI , base64Binary , boolean , date , dateTime , decimal, double , duration , float , gDay , gMonth , gMonthDay , gYear , gYearMonth , hexBinary , NOTATION (note 1), QName (note 2), string , time , untypedAtomic |
time | time , string , untypedAtomic |
untypedAtomic | anyURI , base64Binary , boolean , date , dateTime , decimal , double , duration , float , gDay , gMonth , gMonthDay , gYear , gYearMonth , hexBinary , string , time , untypedAtomic |
Note 1: Casting from a string to
xs:NOTATION
itself is not allowed, because
xs:NOTATION
is an abstract type. Casting to a user-defined subtype of
xs:NOTATION
is permitted, but only if the string is written as a string literal.
Note 2: Casting from a string to
xs:QName
is allowed only if the string is written as a string literal. This means that the cast can always be done at compile time, which avoids problems in deciding the appropriate namespace context to use for the conversion.
Converting from anyURI
Destination Type | Rules |
anyURI | The value is returned unchanged. |
string | The value is returned as a string containing exactly the same characters as the supplied anyURI value. No percent-encoding or -decoding of special characters is performed. |
untypedAtomic | Returns the same result as converting to a string, but the result is labeled as untypedAtomic . |
Converting from base64Binary
Destination Type | Rules |
base64Binary | The value is returned unchanged. |
hexBinary | A hexBinary value is constructed containing the same octets as the original base64Binary value. |
string | The canonical lexical representation of the base64Binary value is returned, as a string. This representation is defined in Part 2 of the XML Schema specification (be sure to read the latest version, the original has been corrected). It outputs the value with no whitespace. |
untypedAtomic | Returns the same result as converting to a string, but the result is labeled as untypedAtomic . |
Converting from boolean
Destination Type | Rules |
boolean | The value is returned unchanged. |
decimal | true is converted to 1.0 , false to 0.0 . |
double | true is converted to 1.0e0 , false to 0.0e0 . |
float | true is converted to xs:float(1.0e0) , false to xs:float(0.0e0) . |
string | Returns the string “true” or “false” . |
untypedAtomic | Returns the same result as converting to a string, but the result is labeled as untypedAtomic . |
Converting from date
Destination Type | Rules |
date | The value is returned unchanged. |
dateTime | Returns the dateTime representing the instant in time at which the relevant date starts. The timezone (or the absence of a timezone) is retained unchanged. For example, the date 2008-04-04 becomes the dateTime 2008-04-04 T00:00:00 . |
gDay | Returns a gDay value containing the same day component and timezone (or absence of a timezone) as the original date. |
gMonth | Returns a gMonth value containing the same month component and timezone (or absence of a timezone) as the original date. |
gMonthDay | Returns a gMonthDay value containing the same month and day components and timezone (or absence of a timezone) as the original date. |
gYear | Returns a gYear value containing the same year component and timezone (or absence of a timezone) as the original date. |
gYearMonth | Returns a gYearMonth value containing the same year and month components and timezone (or absence of a timezone) as the original date. |
string | Returns the canonical lexical representation of the date, retaining the original timezone. For example, a date with no timezone might be converted to the string 2008-06-19 , while a date in the Pacific timezone might become 2008-06-19-08:00 . The timezone is represented as ±hh:mm , except for UTC which is represented by the single letter Z . |
untypedAtomic | Returns the same result as converting to a string, but the result is labeled as untypedAtomic . |