Chapter 8. Functions

Functions are a useful feature of XQuery that allow a wide array of built-in functionality, as well as the ability to modularize and reuse parts of queries. There are two kinds of functions: built-in functions and user-defined functions.

Built-in Versus User-Defined Functions

The built-in functions are a standard set supported by all XQuery implementations. A detailed description of each built-in function is provided in Appendix A, and most are also discussed at appropriate places in the book.

A user-defined function is one that is specified by a query author, either in the query itself, or in an external library. The second half of this chapter explains how to define your own functions in detail.

Calling Functions

The syntax of a function call, shown in Figure 8-1, is the same whether it is a built-in function or a user-defined function. It is the qualified name of the function, followed by a parenthesized list of the arguments, separated by commas. An argument is the actual value that is passed to a function, while a parameter is its definition. For example, to call the substring function, you might use:

substring($prodName, 1, 5)
Figure 8-1. Syntax of a function call

Function calls can be included anywhere an expression is permitted. For example, you might include a function call in a let clause, as in:

let $name := substring($prodName, 1, 5)

or in element constructor ...

Get XQuery, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.