Type promotion

Type promotion is a process that converts an atomic value from its original type to the type that is expected by an expression. XQuery uses type promotion during the evaluation of function calls, order by clauses, and operators that accept numeric or string operands.

XQuery permits the following type promotions:
Numeric type promotion:
A value of type xs:float (or any type that is derived by restriction from xs:float) can be promoted to the type xs:double. The result is the xs:double value that is the same as the original value.
A value of type xs:decimal (or any type that is derived by restriction from xs:decimal) can be promoted to either of the types xs:float or xs:double. The result of this promotion is created by casting the original value to the required type. This kind of promotion might cause loss of precision.
In the following example, a sequence that contains the xs:double value 13.54e-2 and the xs:decimal value 100 is passed to the fn:sum function, which returns a value of type xs:double:
fn:sum(xs:double(13.54e-2), xs:decimal(100))
URI type promotion:
A value of type xs:anyURI (or any type that is derived by restriction from xs:anyURI) can be promoted to the type xs:string. The result of this promotion is created by casting the original value to the type xs:string.
In the following example, the URI value is promoted to the expected type xs:string, and the function returns 18:
fn:string-length(xs:anyURI("http://example.com"))
Note that type promotion and subtype substitution differ in the following ways:
  • For type promotion, the atomic value is actually converted from its original type to the type that is expected by an expression.
  • For subtype substitution, an expression that expects a specific type can be invoked with a value that is derived from that type. However, the value retains its original type.