• +49-(0)721-402485-12
Your experts for XML, XQuery and XML Databases

Type conversion

Apart from the constructor for each type, there is a cast as operator which fulfils the same function, namely to generate a value of a certain type from an output value:

"123" cast as xs:integer

Not every type can be cast in another type by means of cast as or a constructor. For the primitive types of XML Schema, XQuery determines that xs:string can be cast in almost every type (if its lexical representation is taken into account) and vice versa. The three numerical types xs:float, xs:double and xs:decimal can also be cast in one another if the respective value is within the value range of the target type. For values of derived types (such from XML Schema as well as such defined by the user), XQuery allows the following conversions (provided that the value is within the value range of the target type):

  • from a type derived by restriction to a (direct or indirect) original type and also vice versa,
  • between two derived types if the underlying primitive types from XML Schema can be cast in one another.

Type conversion from and to xs:boolean

A number (based on xs:double, xs:float or xs:decimal) can be converted into a value of xs:boolean type. The value false occurs if the number is 0 or NaN (not a number), otherwise true. Likewise, a value of xs:boolean type can be converted into a number (false becomes 0, true becomes 1).

Type conversion from xs:double to xs:float

If a value of xs:double type is converted into a value of xs:float type which has a smaller value range, an output value which is higher than the upper limit of the value range for xs:float does not result in an error, but in the value +INF. Analogously, the value -INF occurs for values being less than the smallest value which is presentable as xs:float.

Type conversion to xs:decimal and xs:integer

If a value of xs:float or xs:double type is converted into xs:decimal, the nearest number from the value range of xs:decimal is selected. If a value of the types xs:float, xs:double or xs:decimal is converted into xs:integer, the decimal portion is omitted (which means no rounding takes place):

xs:integer(19.999) = 19

Type conversion of a general sequence

Since a cast as cannot only be applied on a literal but also on any expression, it may happen that the value of this expression is a sequence with more than one item. In this case, an error is reported.

It may also happen that the value of the expression to be converted is an empty sequence. In this case, too, an error is generated unless this case would have been expressly permitted by indicating a question mark (analogous to the frequency information in DTD) after the target type. The expression

() cast as xs:integer

generates an error, whereas

() cast as xs:integer?

does not result in an error but returns an empty sequence.

 

Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>