当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery 函数

Xquery
XQuery 介绍
XQuery 实例
XQuery FLWOR 表达式
XQuery FLWOR + HTML
XQuery 术语
XQuery 语法
XQuery 添加元素和属性
XQuery 选择和过滤
XQuery 函数
XQuery 摘要
XQuery 参考

Xquery 中的 XQuery 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 692 ::
收藏到网摘: n/a

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 Functions
XQuery 函数

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内定义自己的函数。

XQuery Built-in Functions
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内置函数参考


Examples of Function Calls
函数调用实例

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))

 


XQuery User-Defined Functions
XQuery 用户自定义函数

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.
用户自定义函数可以在查询语句或独立库中进行定义。

Syntax
语法

declare function prefix:function_name($parameter AS datatype)
AS returnDatatype
{
(: ...function code here... :)
};

Notes on user-defined functions:
用户自定义函数需要注意以下几点:

  • Use the declare function keyword
    使用 “declare function(函数声明)” 关键词
  • The name of the function must be prefixed
    函数名称要必须包含前缀
  • The data type of the parameters are mostly the same as the data types defined in XML Schema
    参数的数据类型要和在XML Schema中定义的数据类型基本一致
  • The body of the function must be surrounded by curly braces
    函数的主体部分必须在圆括号内书写

Example of a User-defined Function Declared in the Query
在查询语句里声明的用户自定义函数案例

declare function local:minPrice(
$price as xs:decimal?,
$discount as xs:decimal?)
AS xs:decimal?
{
let $disc := ($price * $discount) div 100
return ($price - $disc)
};
(: Below is an example of how to call the function above :)
<minPrice>{local:minPrice($book/price, $book/discount)}</minPrice>

评论 (0) All

登陆 | 还没注册?