当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery 函数
XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.
XQuery 1.0、XPath 2.0 和 XSLT 2.0 包含相同的函数库。
XQuery includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more. You can also define your own functions in XQuery.
XQuery 包括超过100个内置函数,具体如下:字符串值、数值、日期时间值、节点以及QName操作、排序操作、逻辑值等函数。你也可以在XQuery内定义自己的函数。
The URI of the XQuery function namespace is:
http://www.w3.org/2005/02/xpath-functions
XQuery函数名称空间(namespaces)的URI是:
http://www.w3.org/2005/02/xpath-functions
The default prefix for the function namespace is fn:.
默认的函数命名空间前缀是fn:.
Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is the default prefix of the namespace, the function names do not need to be prefixed when called.
提示:函数常常以“fn:”为前缀名调用,例如fn:string();然而,因为fn:是命名空间的默认前缀,所以这个函数的名称并不需要通过前缀名来调用。
The reference of all the built-in XQuery 1.0 functions is located in our XPath tutorial.
XQuery 1.0内置函数参考。
A call to a function can appear where an expression may appear. Look at the examples below:
函数调用可以出现在一个表达式中所有可能出现的地方,看下面的案例:
Example 1: In an element
例1:在一个元素中:
<name>{uppercase($booktitle)}</name> |
Example 2: In the predicate of a path expression
例2:在路径表达的条件谓语项中:
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry'] |
Example 3: In a let clause
例3:在let子句中:
let $name := (substring($booktitle,1,4)) |
If you cannot find the XQuery function you need, you can write your own.
如果你找不到需要使用的XQuery函数,你可以自己书写。
User-defined functions can be defined in the query or in a separate library.
用户自定义函数可以在查询语句或独立库中进行定义。
declare function prefix:function_name($parameter AS datatype) (: ...function code here... :) }; |
Notes on user-defined functions:
用户自定义函数需要注意以下几点:
declare function local:minPrice( (: Below is an example of how to call the function above :) <minPrice>{local:minPrice($book/price, $book/discount)}</minPrice> |