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.[*] For example, to call the substring function, you might use:

substring($prodName, 1, 5)
Syntax of a function call

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 content:

<name>{substring($prodName, 1, 5)}</name>

or in the predicate of a path expression:

doc("catalog.xml")/catalog/product[substring(name, 1, 5) = 'Short']

Function Names

Functions have namespace-qualified names. All of the built-in function names are in the XPath Functions Namespace, http://www.w3.org/2005/xpath-functions. Since this is the default namespace for functions, the built-in functions can be referenced without a namespace prefix (unless you have overridden the default function namespace, which is not recommended). Some XQuery users still prefer to use the fn prefix for these functions, but this is normally unnecessary.

If the function is user-defined, it must be called by its prefixed name. If a function is declared in the same query module, you can call it using the same prefixed name found in ...

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