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.
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 is derived by restriction from
ProductType. Note that the value retains its original type; it is not actually cast to another type.
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: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 ...