Latest web development tutorials

XQuery Functions

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


XQuery Functions

XQuery contains over 100 built-in functions. These functions can be used for string values, numeric, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and so on. You can also define your own functions in XQuery.


XQuery built-in functions

XQuery function namespace URI:

http://www.w3.org/2005/02/xpath-functions

The default function namespace prefix is ​​fn :.

Tip: call prefix, such as fn:: function often through fn string (). However, since fn: is the default prefix of the namespace, the function names do not need to use the prefix when called.

You can find the complete "in our XPath tutorial built-in XQuery Functions Reference Manual ."


Examples of function calls

Function calls can be used with an expression. Consider the following example:

Example 1: The element

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

Example 2: In the path expression in the predicate

doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']

Example 3: let statement

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


User-defined functions XQuery

If you can not find the required XQuery function, you can write your own functions.

Or users can define separate database query custom function.

grammar

declare function前缀:函数名($参数 AS 数据类型)
AS返回的数据类型
{
...函数代码...
}

About user-defined functions Notes:

  • Please use the declare function keyword
  • Function names must use the prefix
  • Data type parameter is generally consistent with the data defined in the XML Schema type
  • Function body must be surrounded by curly braces

In a query statement user-defined function examples:

declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)
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>