Automatic Type Conversions

In XQuery, each function and operator expects its arguments to be of a particular type. However, this is not as rigid as it may sound since there are a number of type conversions that happen automatically. They are discussed in this section.

Subtype Substitution

Functions and operators that expect a value of a particular type also accept a value of one of its derived types. This is known as subtype substitution. For example, the upper-case function expects an xs:string as an argument, but you can pass a value whose type is derived by restriction from xs:string, such as xs:NMTOKEN. This also works for complex types. A function expecting an element of type ProductType also accepts an element of type UmbrellaType, if UmbrellaType is derived by restriction from ProductType. Note that the value retains its original type; it is not actually cast to another type.

Type Promotion

When two values of different numeric types are compared or used in the same operation, one is promoted to the type of the other. An xs:decimal value can be promoted to the xs:float or xs:double type, and an xs:float value can be promoted to the xs:double type. For example, the expression 1.0 + 1.2E0 adds an xs:decimal value (1.0) to an xs:double value. The xs:decimal value is promoted to xs:double before the expression is evaluated. Numeric type promotion happens automatically in arithmetic expressions, comparison expressions, and function calls.

In addition, values of type xs:anyURI are ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.