Skip to content

XQuery Functions

Explore the built-in and user-defined functions in XQuery 1.0, XPath 2.0, and XSLT 2.0, including syntax, data types, and practical examples for XML processing.

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.

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

<name>{upper-case($booktitle)}</name>

In the Predicate of a path expression

doc("books.xml")/bookstore/book[substring(author,1,3)='Tom']

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_...
};

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:

<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>
Navigation