以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XQuery/XLink/XPointer/ 』  (http://bbs.xml.org.cn/list.asp?boardid=14)
----  XQuery从头学  (http://bbs.xml.org.cn/dispbbs.asp?boardid=14&rootid=&id=45336)


--  作者:xml-linguist
--  发布时间:4/12/2007 3:33:00 PM

--  XQuery从头学
Let's try to learn some basic XQuery syntax by looking at an example.
从实例直接开始。

We will use the following XML document in the examples below.
先看下面的XML文件

"books.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>
--------------------------------------------------------------------------------

How to Select Nodes From "books.xml"?
从"books.xml"中选取结点

Functions
XQuery uses functions to extract data from XML documents.
XQuery利用函数从XML文件中提取数据。(注:本人非理科、非工科、非IT行业,我一看见Functions就会想到“功能”。今天学一个行话,“函数”,不知合适不合适。)

The doc() function is used to open the "books.xml" file:
用doc() 来打开"books.xml"文档:
doc("books.xml")

Path Expressions
XQuery uses path expressions to navigate through elements in an XML document.
XQuery采用路径表达式在XML文件的元素中导航。

The following path expression is used to select all the title elements in the "books.xml" file:
用以下路径表达式在 "books.xml"文档中选取所有的"title"元素:
doc("books.xml")/bookstore/book/title

(/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element)
(/bookstore选取"bookstore"元素;/book选取"bookstore"元素下属的所有"book"元素;/title选取每个"bookstore"元素下属的所有"title"元素)

The XQuery above will extract the following:
以上XQuery提取以下数据:

<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

Predicates
XQuery uses predicates to limit the extracted data from XML documents.
谓词
XQuery利用谓词来限定从XML文件中提取的数据。

The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:
以下谓词用以选取"bookstore"元素下属的、拥有"price"元素且"price"元素值小于30的所有"book"元素:
doc("books.xml")/bookstore/book[price<30]

The XQuery above will extract the following:
上述XQuery提取以下数据:

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

[此贴子已经被作者于2007-4-14 18:18:20编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:38:00 AM

--  
XQuery FLWOR Expressions
XQuery FLWOR表达式

The XML Example Document
We will use the "books.xml" document in the examples below.
以下是要用到的XML文件。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--  Edited with XML Spy v2007 (http://www.altova.com) -->
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>.
--------------------------------------------------------------------------------
How to Select Nodes From "books.xml" With FLWOR
利用FLWOR在"books.xml"中选取结点

Look at the following path expression:
先看以下的路径表达式:

doc("books.xml")/bookstore/book[price>30]/title

The expression above will select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30.
(这个表达式的含义上一节里已经说明过,不再赘述。)

The following FLWOR expression will select exactly the same as the path expression above:
利用以下FLWOR表达式可以选取的结点和上述路径表达式选取的结点完全一致:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title

The result will be:
结果如下:

<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

With FLWOR you can sort the result:
利用FLWOR可以对结果内容进行分类/排序:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

FLWOR is an acronym for "For, Let, Where, Order by, Return".
FLWOR是"For, Let, Where, Order by, Return"四个指令的首字母缩略词。

The for clause selects all book elements under the bookstore element  into a variable called $x.
for指令分句选取bookstore元素下属的所有book元素,将其归于$x变量。

The where clause selects only book elements with a price element with a value greater than 30.
where指令分句仅选取含有price元素且price值大于30的book元素。

The order by clause defines the sort-order. Will be sort by the title element.
order by指令分句定义分类顺序,此处是按照title元素来排序的。(注:原文出自w3cschoo;该句中的Will be sort是明显的错误,应该是Will be sorted。)

The return clause specifies what should be returned. Here it returns the title elements.
return指令分句确定要返回的值。此处返回的的title元素。

The result of the XQuery expression above will be:
以上XQuery表达式的最后效果如下:

<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

(注:由于使用了order by指令,title元素值的顺序按字母顺序排列了。)


--  作者:xml-linguist
--  发布时间:4/13/2007 9:40:00 AM

--  
XQuery FLWOR + HTML
FLWOR表达式和HTML的综合应用
-------------------------------------------------------------------------------

The XML Example Document
We will use the "books.xml" document in the examples below.
先看以下XML文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--  Edited with XML Spy v2007 (http://www.altova.com)-->
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
--------------------------------------------------------------------------------

Present the Result In an HTML List
用HTML列表显示结果

Look at the following XQuery FLWOR expression:
先看以下XQuery的FLWOR表达式:

for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x

The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order.
上述表达式选取bookstore元素下属的book元素所包含的所有title元素,返回值为以字母顺序排序的title元素。

Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul> and <li> tags to the FLWOR expression:
现在把bookstore中的所有书名用HTML列表排列。给FLWOR表达式中添加<ul>和<li>标签:

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

The result of the above will be:
以上表达式的最后效果如下:

<ul>
<li><title lang="en">Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</ul>

Now we want to eliminate the title element, and show only the data inside the title element:
现在再清除title元素名称,只显示title元素的值:

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>

The result will be (an HTML list):
最终效果如下(成了一个HTML表格):

<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>


[此贴子已经被作者于2007-4-13 10:10:37编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:40:00 AM

--  
XQuery Terms
XQuery表达方式

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
XQuery有七种结点:element(元素),attribute(属性),text(文本),namespace(名称空间),processing-instruction(处理指令),comment(注释)和document (root)(文件或根)结点。
--------------------------------------------------------------------------------

XQuery Terminology
XQery术语
Nodes
结点

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).
如上所述,XQuery有七种结点。XML文件被看作是由结点构成的树。树根就是文件结点(或根结点)。

Look at the following XML document:
请看以下XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book></bookstore>

Example of nodes in the XML document above:
以上XML文件中的结点示例:

<bookstore>  (document node)
<author>J K. Rowling</author>  (element node)
lang="en"  (attribute node)
<bookstore>(文件结点)
<author>J K. Rowling</author>(元素结点)
lang="en"(属性结点)

Atomic values
原子值
Atomic values are nodes with no children or parent.
原子值指的是没有子结点或父结点的结点。

Example of atomic values:
原子值示例:
J K. Rowling
"en"

Items

Items are atomic values or nodes.
项就是原子值或结点。
--------------------------------------------------------------------------------

Relationship of Nodes
结点之间的关系

Parent
父结点
Each element and attribute has one parent.
一个元素和属性有一个父结点。

In the following example; the book element is the parent of the title, author, year, and price:
以下示例中的book元素是title,author,year和price元素的父结点:
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Children
子结点
Element nodes may have zero, one or more children.
元素结点可以拥有0个,1个或多个子结点。

In the following example; the title, author, year, and price elements are all children of the book element:
以下示例中的title,author,year和price元素都是book元素的子结点。

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Siblings
兄弟结点
Nodes that have the same parent.
拥有共同父结点的结点就是兄弟结点。

In the following example; the title, author, year, and price elements are all siblings:
以下示例中的title,author,year和price元素都是兄弟结点:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Ancestors
祖结点
A node's parent, parent's parent, etc.
一个结点的父结点、父结点的父结点,依次类推,都是该结点的祖结点。

In the following example; the ancestors of the title element are the book element and the bookstore element:
以下示例中title元素的祖结点有book元素和bookstore元素。

<bookstore><book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book></bookstore>

Descendants
子孙接点
A node's children, children's children, etc.
一个接点的子结点、子结点的子结点,依次类推,都是给接点的子孙结点。

In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
以下示例中bookstore的子孙结点包括book,title,auther,year和price元素:

<bookstore><book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book></bookstore>

[此贴子已经被作者于2007-4-15 7:59:59编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:40:00 AM

--  
XQuery Syntax
XQuery语法  
--------------------------------------------------------------------------------

XQuery is case-sensitive and XQuery elements, attributes, and variables must be valid XML names.
XQuery区分大小写;XQuery中的元素、属性和变量必须是有效的XML名称。
--------------------------------------------------------------------------------

XQuery Basic Syntax Rules
Some basic syntax rules:
XQuery基本语法规则

XQuery is case-sensitive
XQuery elements, attributes, and variables must be valid XML names
An XQuery string value can be in single or double quotes
An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
区分大小写
元素、属性和变量必须是有效的XML名称
属性值可以置于单引号或双引号之中
变量的定义方式为$后面跟名称,如$bookstore
注释由(:和:)分别前后定界,如(: XQuery Comment :)
--------------------------------------------------------------------------------

XQuery Conditional Expressions
XQuery条件表达式
"If-Then-Else" expressions are allowed in XQuery.
XQuery中允许使用"If-Then-Else"表达式。

Look at the following example:
看下列:
(注:引用的示例文件还是前面出现过的"books.xml")

for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
 then <child>{data($x/title)}</child>
 else <adult>{data($x/title)}</adult>

Notes on the "if-then-else" syntax: parentheses around the if expression are required. else is required, but it can be just else ().
"if-then-else"语法的说明:if表达式要求被置于圆括号中;else不能缺,但可以仅仅是else ()的形式。

The result of the example above will be:
以上示例的效果如下:

<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>


--------------------------------------------------------------------------------

XQuery Comparisons
XQuery对比关系

In XQuery there are two ways of comparing values.
XQuery用两种方法来实现值的对比。

1. General comparisons: =, !=, <, <=, >, >=
    通用比对法:=, !=, <, <=, >, >=

2. Value comparisons: eq, ne, lt, le, gt, ge
    值比对法:eq, ne, lt, le, gt, ge

The difference between the two comparison methods are shown below.
两种对比方法之间的差别见下文。

Look at the following XQuery expressions:
先看以下XQuery表达式:

$bookstore//book/@q>10
The expression above returns true if any q attributes have values greater than 10.
在上述表达式中,任意一个q属性值大于10的时候,就返回"true"值。

$bookstore//book/@q gt 10
The expression above returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs.
在上述表达式中,如果只有一个q属性值返回,并且其值大于10,那就是"true";如果返回多于一个q,那就发生错误。

(注:最后两段的翻译有点照猫画虎了,因为我自己对原文吃不透了。请各位把正确的理解和表达方式跟贴发出,我再更改。多谢了!)

[此贴子已经被作者于2007-4-14 9:47:52编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:40:00 AM

--  
XQuery Adding Elements and Attributes
XQuery添加元素和属性
--------------------------------------------------------------------------------
The XML Example Document
We will use the "books.xml" document (same XML file as in the previous chapters).
还是以前面几节出现的"books.xml"文件为示例。
--------------------------------------------------------------------------------

Adding Elements and Attributes to the Result
给最终效果中添加元素和属性

As we have seen in a previous chapter, we may include elements and attributes from the input document ("books.xml) in the result:
在前面几节已经了解了如何从输入文件(即("books.xml)中选取元素和属性在最后效果中显示出来:

for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x

The XQuery expression above will include both the title element and the lang attribute in the result, like this:
上述XQuery表达式将在最终效果中包括title元素和lang属性,显示如下:

<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

The XQuery expression above returns the title elements the exact same way as they are described in the input document.
上述XQuery表达式所返回的title元素的内容和输入文件中所描述的完全一致。

We now want to add our own elements and attributes to the result!
现在,给最终效果中添加我们自己的元素和属性!

Add HTML Elements and Text
添加HTML元素和文本

Now, we want to add some HTML elements to the result. We will put the result in an HTML list - together with some text:
现在给最终结果中添加一些HTML元素。最终效果将以HTML列表的格式显示,同时还附带一些文本内容:

<html>
<body><h1>Bookstore</h1><ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul></body>
</html>

The XQuery expression above will generate the following result:
上述XQuery表达式将生成以下效果:

<html>
<body><h1>Bookstore</h1><ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul></body>
</html>

Add Attributes to HTML Elements
给HTML元素添加属性
Next, we want to use the category attribute as a class attribute in the HTML list:
接下来把输入文件中的category属性转换成HTML列表中的class属性:

<html>
<body><h1>Bookstore</h1><ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)}</li>
}
</ul></body>
</html>

The XQuery expression above will generate the following result:
上述XQuery表达式生成以下效果:

<html>
<body><h1>Bookstore</h1><ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul></body>
</html>

[此贴子已经被作者于2007-4-14 11:16:59编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:40:00 AM

--  
XQuery Selecting and Filtering
XQuery选择和过滤  
--------------------------------------------------------------------------------
The XML Example Document
XML实例文档
We will use the "books.xml" document in the examples below (same XML file as in the previous chapters).
我们使用这个"books.xml"文档(和上面的几节中所使用的XML文件相同)。
--------------------------------------------------------------------------------
Selecting and Filtering Elements
选择和过滤元素

As we have seen in the previous chapters, we are selecting and filtering elements with either a Path expression or with a FLWOR expression.
前面几节已经讲过,选择和过滤元素时要么使用Path表达式要么使用FLWOR表达式。

Look at the following FLWOR expression:
看下面的FLWOR表达式:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

for - (optional) binds a variable to each item returned by the in expression
let - (optional)
where - (optional) specifies a criteria
order by - (optional) specifies the sort-order of the result
return - specifies what to return in the result

for(可选) 为每个由in表达式返回的项绑定一个变量
let(可选)
where(可选)设定标准(选择条件)
order by (可选)设定结果的排列顺序
return 确定返回于结果中的内容


The for Clause
for分句
The for clause binds a variable to each item returned by the in expression. The for clause results in iteration. There can be multiple for clauses in the same FLWOR expression.
for分句用以绑定由in表达式返回的每个项目的变量。For分句可产生叠代。在同一个FLWOR表达式中可存在多重for分句。

To loop a specific number of times in a for clause, you may use the to keyword:
如果要在一个for分句中循环特定次数,需要使用关键词to:

for $x in (1 to 5)
return <test>{$x}</test>

Result:
结果:

<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>

The at keyword can be used to count the iteration:
关键词at可被用来计算叠代:

for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>

Result:
结果:

<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>

It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression:
for分句中可以出现多个in表达式。这时,可以用逗号来把每个in表达式分割开来:

for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>

Result:
结果:

<test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>

The let Clause
let分句
The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.
let分句允许变量分配,可以避免同一个表达式的多次重复。let分句不会产生叠代。

let $x := (1 to 5)
return <test>{$x}</test>

Result:
结果:

<test>1 2 3 4 5</test>

The where Clause
where分句
The where clause is used to specify one or more criteria for the result:
where分句用来为结果确定一个或多个标准。

where $x/price>30 and $x/price<100

The order by Clause
order by分句
The order by clause is used to specify the sort order of the result. Here we want to order the result by category and title:
order by分句用以确定结果中的排次顺序。下例以category和title来对结果排序:

for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title

Result:
结果:

<title lang="en">Harry Potter</title>
<title lang="en">Everyday Italian</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

The return Clause
return分句
The return clause specifies what is to be returned.
return分句确定返回的内容。

for $x in doc("books.xml")/bookstore/book
return $x/title

Result:
结果:

<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>


[此贴子已经被作者于2007-4-16 10:58:11编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 9:41:00 AM

--  
XQuery Functions
  
--------------------------------------------------------------------------------

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


--------------------------------------------------------------------------------

XQuery Functions
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 Built-in Functions
The URI of the XQuery function namespace is:
http://www.w3.org/2005/02/xpath-functions

The default prefix for the function namespace is 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.

The reference of all the built-in XQuery 1.0 functions is located in our XPath tutorial.


--------------------------------------------------------------------------------

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

<name>{uppercase($booktitle)}</name>

Example 2: In the predicate of a path expression

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

Example 3: In a let clause

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


--------------------------------------------------------------------------------

XQuery User-Defined Functions
If you cannot find the XQuery function you need, you can write your own.

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

XQuery 函数
XQuery含有超过100个内建的函数。这些函数可用于字符串值、数值、日期以及时间比较、节点和QName操作、序列操作、逻辑值等等。您也可在XQuery中定义自己的函数。
XQuery 内建函数
XQuery函数命名空间的URI:

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

函数命名空间的默认前缀是fn:。

提示:函数经常被通过fn:前缀进行调用,例如fn:string()。不过,由于fn:是命名空间的默认前缀,所以函数名称不必在被调用时使用前缀。

您可以在我们的XPath教程中找到完整的《内建XQuery函数参考手册》。
函数调用实例
函数调用可与表达式一同使用。请看下面的例子:

例1:在元素中
<name>{uppercase($booktitle)}</name>例2: 在路径表达式的谓语中
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']例3: 在let语句中
let $name := (substring($booktitle,1,4))XQuery 用户定义函数
如果找不到所需的XQuery函数,你可编写自己的函数。

可在查询中或独立的库中定义用户自定义函数。

语法
declare function 前缀:函数名($参数 AS 数据类型)
  AS 返回的数据类型
{

(: ...函数代码... :)

};关于用户自定义函数的注释:
请使用declare function关键词
函数名须使用前缀
参数的数据类型通常与在XML Schema中定义的数据类型一致
函数主体须被花括号包围
一个在查询中声明的用户自定义函数的例子:
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>

(注:这一块学了两遍,有点搞不清,直接抄了别人的翻译资料过来。再学学看!)

[此贴子已经被作者于2007-4-18 11:05:53编辑过]

--  作者:xml-linguist
--  发布时间:4/13/2007 3:04:00 PM

--  
You Have Learned XQuery, Now What?
已经学习了XQuery,下一步干什么?
--------------------------------------------------------------------------------

XQuery Summary
XQuery总结

This tutorial has taught you how to query XML data.
这一节学了如何查询XML数据。

You have learned that XQuery was designed to query anything that can appear as XML, including databases.
已经清楚,XQuery的设计意图就是查询出现在XML文件中的任何内容,包括数据库。

You have also learned how to query the XML data with FLWOR expressions, and how to construct XHTML output from the collected data.
同时还学了如何运用FLWOR表达式查询XML数据、如何利用采集的数据建立XHTML输出。

For more information on XQuery, please look at our XQuery Reference.
关于XQuery,请看XQuery Reference可以获得更多信息。

--------------------------------------------------------------------------------
Now You Know XQuery, What's Next?
已经学了XQuery,下一步该怎么办呢?

The next step is to learn about XLink and XPointer.
下面就可以学XLink和XPointer了。

Linking in XML is divided into two parts: XLink and XPointer.
XML中的链接分为两个部分:XLink和XPointer。

XLink and XPointer define a standard way of creating hyperlinks in XML documents.
XLink和XPointer确立了在XML文件中建立超级链接的标准方式。

If you want to learn more about XLink and XPointer, please visit XLink and XPointer tutorial.
要学XLink和XPointer,请继续学——

[此贴子已经被作者于2007-4-18 11:19:05编辑过]

--  作者:jx
--  发布时间:4/13/2007 8:21:00 PM

--  
欢迎,请继续。
--  作者:xml-linguist
--  发布时间:4/13/2007 9:35:00 PM

--  
谢谢!我会坚持下去的。
我自己要学一遍,顺便贴在这里,希望对初学的人有帮助。
--  作者:xml-linguist
--  发布时间:4/13/2007 9:37:00 PM

--  
有些术语我拿不准,希望大家指出来。
--  作者:cuiyaxin
--  发布时间:4/13/2007 10:53:00 PM

--  
你贴的是什么呢
如果是适合初学者的
请给我一份巴
cyx2121921@sina.com
--  作者:xml-linguist
--  发布时间:4/14/2007 9:52:00 AM

--  
以下是引用cuiyaxin在2007-4-13 22:53:00的发言:
你贴的是什么呢
如果是适合初学者的
请给我一份巴
cyx2121921@sina.com

对不起cuiyaxin!我是边学边做,直接在网络上做,我自己都没有备份。你直接看就行了,如果需要,麻烦你动手copy一下吧!


--  作者:jx
--  发布时间:4/15/2007 2:20:00 PM

--  
支持!
鼓励你!
--  作者:xml-linguist
--  发布时间:4/16/2007 11:35:00 AM

--  
谢谢支持!
学完以后,应该实践了,估计难度更大一点。
只有实践,才能真正学会!
--  作者:jx
--  发布时间:4/19/2007 7:26:00 AM

--  
继续吧,很好啊咯!
--  作者:jingle_even
--  发布时间:4/30/2007 3:05:00 PM

--  
楼主辛苦了!UP UP!
--  作者:smilemagi
--  发布时间:5/9/2007 8:06:00 PM

--  
http://www.w3schools.com/xquery/default.asp

lz应该是在这里看的,这个网站是个很好的初学者网站,而且包含大多编程语言的初学教程^^
我在uni里面^^老师都给我介绍^^
本人很喜欢^^呵呵
希望对你有帮助^^


--  作者:jiafan
--  发布时间:5/21/2007 1:16:00 PM

--  
好人啊
辛苦
--  作者:jx
--  发布时间:6/3/2007 12:52:00 PM

--  
继续写出来。
--  作者:mycatboys
--  发布时间:6/26/2007 8:20:00 PM

--  
支持

------------------------------------------------------------------------------------------------------------------
我做了中资源[url=http://www.118cy.net/host/]虚拟主机[/url]的小代理,想注册[url=http://www.118cy.net/help/webHelp_host_1002_0.htm]空间注册[/url]的朋友MSN,兼职赚点外快


--  作者:lish
--  发布时间:9/12/2007 4:08:00 PM

--  
thans very much
--  作者:jx
--  发布时间:9/23/2007 6:25:00 PM

--  
请继续。
--  作者:龙藤
--  发布时间:10/16/2007 3:39:00 PM

--  
很好,谢谢
--  作者:feathen1983
--  发布时间:10/16/2007 8:24:00 PM

--  xquery应该写在哪?
请问XQUERY的平台是什么?了解了语法和其他相关知识,但还是不知道该把语句写在哪才能显示出查询结果
--  作者:viva156
--  发布时间:12/19/2007 2:14:00 AM

--  
一个问题
我不知道怎么执行xquery。。。
比较郁闷
表达式基本上可以看懂
我用xmlbean
执行老是说没有找到query引擎

--  作者:kevin86713
--  发布时间:1/15/2008 10:46:00 PM

--  
太好了~~
楼主加油~!
--  作者:xml-linguist
--  发布时间:7/28/2008 9:07:00 AM

--  
其实,我根本不懂XQuery是干什么的。
在我做资料的过程中,有些构思很难一步到位:任何一个方案都要花去我大量的时间,而无论如何最终的结果还是无法令人满意。
学习XQuery纯粹是未来了却心愿:既然学xml,顺便把该知道的都学学吧!我就是这样才在一年前敷衍了事的看了一边XQuery,但是实际应用才是最近的事。
现在回头一看,我一前做的资料,无论多么不理想,不需再重新升级,只用XQuery把所需的数据抽取出来就好了。我现在只能做这一步。
--  作者:venee_lee
--  发布时间:10/8/2008 8:58:00 AM

--  
请问楼主,xquery代码究竟该放在什么地方。可以直接嵌入html吗?我试过,好像不行。有谁用过xmlspy,可以用来调试xquery吗?
--  作者:venee_lee
--  发布时间:10/9/2008 10:32:00 AM

--  
试出来了,把xquery放在后缀为xq的文件中就可以了。我用的是altova XMLspy。感觉还是蛮方便的。
--  作者:yhjhoo
--  发布时间:2/12/2009 3:44:00 PM

--  
完全跟w3cshool上面的内容一样,没有任何创新啊!


所用的论坛也不具有交互性,哎,论坛可以关闭了


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
550.781ms