XQuery Functions
XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same function library.
XQuery Functions
XQuery contains over 100 built-in functions. These functions can be used for string values, numeric values, date and time comparison, node and QName operations, sequence operations, boolean values, etc. You can also define your own functions in XQuery.
XQuery Built-in Functions
The URI of the XQuery function namespace:
http://www.w3.org/2005/02/xpath-functions
The default prefix of the function namespace is fn:.
Tip:Functions are often called with the fn: prefix, for example fn:string(). However, since fn: is the default prefix of the namespace, the function name does not need to use the prefix when called.
You can find the completeBuilt-in XQuery Function Reference Manual》。
Function Call Examples
Function calls can be used together with expressions. See the example below:
Example 1: In an element
Example 2: In the predicate of a path expression
Example 3: In a let statement
XQuery User-defined Functions
If you cannot find the XQuery function you need, you can write your own function.
User-defined functions can be defined in a query or in a standalone library.
Syntax
AS Return data type
{
...Function code...
}
Notes on user-defined functions:
- Use the declare function keyword
- The function name must use a prefix
- The data types of parameters usually match the data types defined in XML Schema.
- The function body must be enclosed in curly braces.
An example of a user-defined function declared in a query:
AS xs:decimal?
{
let $disc := ($p * $d) div 100
return ($p - $disc)
}
Below is an example of how to call the function above:
<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>