Function calls in XQuery
XQuery supports calls to built-in XQuery functions.
Built-in XQuery functions are in the namespace http://www.w3.org/2003/11/xpath-functions. If the function name in the function call has no namespace prefix, the function is considered to be in the default function namespace.
XQuery uses the following process to evaluate functions:
- XQuery evaluates each expression that is passed as an argument in the function call and returns a value for each expression.
- The value that is returned for each argument is converted to the data type that is expected for
that argument. When the expected type is a sequence of zero or more atomic types, XQuery uses the following rules to convert the
value to its expected type:
- The given value is atomized into a sequence of atomic values.
- Each item in the atomic sequence that is of type
xs:untypedAtomicis cast to the expected atomic type. For built-in functions where the expected type is specified asnumeric, arguments of typexs:untypedAtomicare cast toxs:double. - Numeric type promotion is applied to any numeric item in the atomic sequence that can be
promoted to the expected atomic type through numeric type promotion. Numeric items include items of
type
xs:integer,xs:decimal, orxs:double.
- The function is evaluated using the converted values of its arguments. The result of the function call is either an instance of the function's declared return type or an error.
Examples
The following function call retrieves
the first three characters of the pid attribute of a product document:
SELECT XMLQUERY ('declare namespace pos="http://posample.org";
fn:substring(/pos:product/@pid, 1, 3)'
PASSING DESCRIPTION)
FROM T1;