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.
- 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.