Skip to main content

XQuery Functions

XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.

XQuery Functions

XQuery is built on XPath expressions.

XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.


You can also define your own functions in XQuery.

XQuery Data Types

XQuery shares the same data types as XML Schema 1.0 (XSD).

Examples of Function Calls

A call to a function can appear where an expression can appear.

In an Element


In the Predicate of a path expression


In a let Clause

let $name := (substring($booktitle,1,4))

XQuery User-Defined Functions

If you can't find the XQuery function you need, you can write your own.

User-defined functions can be defined in the query or in a separate library.

declare function _prefix:function_name_($_parameter as datatype_)
as _returnDatatype_
..._function code here_...
  • Use the declare function keyword
  • The function name must be prefixed
  • Parameter data type is mostly the same as data types defined in XML Schema
  • Function body must be surrounded by curly brackets

Example ### of a User-defined Function Declared in the XQuery

declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)
as xs:decimal?
let $disc := ($p * $d) div 100
return ($p - $disc)

This function can be called in this way: