Xquery教程

Xquery教程Xquery教程给XQuery最好的解释就是XQuery和XML之间的联系就好比SQL和数据库之间的联系设计出XQurey的目的是为了查询XML中的数据可以把XQurey理解为XMLQurey(查询)XQuery参考你可以找到所有有关操作符号,内置函数以及数据类型的资料XQuery参考内容目录XQuery介绍介绍了XQuery的概念与它的用途XQuery…

Xquery教程
给XQuery最好的解释就是XQuery和XML之间的联系就好比SQL和数据库之间的联系
设计出XQurey的目的是为了查询XML中的数据
可以把XQurey理解为XML Qurey(查询)

XQuery 参考

你可以找到所有有关操作符号,内置函数以及数据类型的资料

XQuery 参考

内容目录

XQuery 介绍
介绍了XQuery的概念与它的用途

XQuery 实例
通过几个举例来演示XQuery

XQuery FLWOR
解释了什么是 XQuery FLWOR 表达式.

XQuery FLWOR + HTML
演示了如何在HTML列表中呈现FLWOR结果

XQuery 术语
解释了有关XQuery的术语

XQuery 语法
解释了一些基础的XQurey语法

在XQuery的结果中加入元素和属性
演示了如何在结果中加入自定义的元素以及属性

XQuery 对元素的选择与过滤
如何使用FLWOR来对元素进行选择和过滤

XQuery 函数
略读有关内置的XQuery函数和操作符,以及如何去建立自定义函数

XQuery 摘要
回顾已经在本章学习到的知识,并会推荐你下一步应该学习的知识

XQuery 介绍
w3pop.com / 2006-09-21

p.gif
XQuery 实例 n.gif

The best way to explain XQuery is to say that XQuery is to XML what SQL is to database tables.
对XQuery最好的解释是:对于XML来说,XQuery就如同一个类似于SQL数据库表格这样的东西;它相当于XML的数据库。

XQuery is designed to query XML data – not just XML files, but anything that can appear as XML, including databases.
XQuery用于查询XML数据 — 不过不仅仅是XML文件,还包括一切以XML形式出现的东西,包括数据库在内。


What You Should Already Know
你应该已经掌握的

Before you continue you should have a basic understanding of the following:
在继续之前你该对以下的有个基本了解。

  • HTML / XHTML
  • XML / XML Namespaces
  • XPath

If you want to study these subjects first, find the tutorials on our Home page.
如想先学上面的东西,请看我们主页的教程


What is XQuery?
什么是XQuery?

  • XQuery is the language for querying XML data
    • XQuery是查询XML数据的语言
  • XQuery for XML is like SQL for databases
    • XQuery相当于XML的SQL数据库
  • XQuery is built on XPath expressions
    • XQuery 是建立在XPath表达式之上的
  • XQuery is defined by the W3C
    • XQuery是由W3C 定义的
  • XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.)
    • 所有重要的数据库引擎(IBM, Oracle, Microsoft, 等等.)都支持XQuery
  • XQuery will become a W3C standard – and developers can be sure that the code will work among different products
    • XQuery将会成为W3C的标准——并且开发者都相信代码将可以在不同的产品之间运行

XQuery is About Querying XML
XQuery就是有关查询XML的

XQuery is a language for finding and extracting elements and attributes from XML documents.
XQuery是用于从XML文档中寻找和提取元素和属性的语言

Here is an example of a question that XQuery could solve:
下面是一个关于XQuery所能解决的问题的例子

“Select all CD records with a price less than $10 from the CD collection stored in the XML document called cd_catalog.xml”
在名为cd_catalog.xml的XML文档中的CD选集中筛选出所有价格低于10美元的CD唱片


XQuery and XPath

XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. If you have already studied XPath you will have no problems with understanding XQuery.
XQuery 1.0和XPath 2.0拥有同样的数据模型并支持相同的功能和操作。如果你已经学习了XPath,理解XQuery.就不成问题了

You can read more about XPath in our XPath Tutorial.
在我们的XPath 教程你可以阅读到关于XPath更多的东西


XQuery – Examples of Use
XQuery使用例子

XQuery can be used to:
XQuery可用于:

  • Extract information to use in a Web Service
    • 摘录在网络服务中使用的信息
  • Generate summary reports
    • 产生综合报告。
  • Transform XML data to XHTML
    • 把XML数据转换成XHTML形式
  • Search Web documents for relevant information
    • 在网络文件中搜索相关信息

XQuery is Not (Yet) a Web Standard
XQuery还不是网络标准

XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath, and XML Schema.
XQuery和一些W3C标准是吻合的,诸如:XML, Namespaces, XSLT, XPath, 和 XML Schema.

However, XQuery 1.0 is not yet a W3C Recommendation (XQuery is a Working Draft). Hopefully it will be a recommendation in the near future.
但是,XQuery 1.0仍不是W3C的参考标准(XQuery只是一份工作草案),相信在不久的将来,它会变成的一套推荐的标准吧。

XQuery 实例
w3pop.com / 2006-09-21

p.gifXQuery 介绍
XQuery FLWOR 表达式 n.gif

Let’s try to learn some basic XQuery syntax by looking at an example.
让我们通过下面这个案例,来学一些基本XQuery语法


The XML Example Document
XML样文

We will use the following XML document in the examples below.
我们将在下面的案例中使用“books.xml”文档。

“books.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>

View the “books.xml” file in your browser.
在你的浏览器中浏览“books.xml”文件。


How to Select Nodes From “books.xml”?
怎样从”books.xml”中选择节点

Functions
函数

XQuery uses functions to extract data from XML documents.
XQuery用函数从XML文档里摘取数据。

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选择每个book元素下的所有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元素值小于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>

XQuery FLWOR 表达式
w3pop.com / 2006-09-21

p.gifXQuery 实例
XQuery FLWOR + HTML n.gif

The XML Example Document
XML样文

We will use the “books.xml” document in the examples below (same XML file as in the previous chapter).
在下面的例子里我们会用到”books.xml”文档(就是和前几章里用到的那份XML文件)

View the “books.xml” file in your browser.
在你的浏览器中浏览“books.xml”


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.
上述表达式会选择在bookstore元素下的book元素下所有price 元素值高于30的title元素

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元素值高于30的book元素

The order by clause defines the sort-order. Will be sort by the title element.
order by子句定义了“分类命令”。会被标题元素分类

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>

XQuery FLWOR + HTML
w3pop.com / 2006-09-21

p.gifXQuery FLWOR 表达式
XQuery 术语 n.gif

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

View the “books.xml” file in your browser.
在你的浏览器中浏览“book.xml”文件。


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

Look at the following XQuery FLWOR expression:
看下面的Query 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:
现在我们想要在 HTML列表中列出书店里的所有book-titles元素。我们在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>

XQuery 术语
w3pop.com / 2006-09-21

p.gifXQuery FLWOR + HTML
XQuery 语法 n.gif

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在XQuery中,有7中不同的节点:元素,属性,文本,名称空间,处理指令,内容,文档(根目录root)节点。


XQuery 术语

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里,有7中不同的节点:元素,属性,文本,名称空间,处理指令,内容,文档(根)节点,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)

Atomic values
“单元素(Atomic)”属性值

Atomic values are nodes with no children or parent.
“单元素”属性值只没有子节点和父节点。

Example of atomic values:
“单元素(Atomic)”属性值例子:

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.
节点元素可拥有任意个数的子类。

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, author, year, 和price元素:

<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>

<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

XQuery 语法
w3pop.com / 2006-09-21

p.gifXQuery 术语
XQuery 添加元素和属性 n.gif

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


XQuery Basic Syntax Rules
XQuery的基本语法规则

Some basic syntax rules:
一些基本语法规则:

  • XQuery is case-sensitive
    • XQuery区分大小写
  • XQuery elements, attributes, and variables must be valid XML names
    • 它的元素,属性,变量必须是有效的XML名称
  • An XQuery string value can be in single or double quotes
    • 一个XQuery字符串值可以写在单引号或双引号里
  • An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
    • 一个XQuery变量定义是在“$”的符号后面跟上名称等,例如:$bookstore
  • XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
    • XQuery注释用“(: ”和“ :)”进行分界,例如(: XQuery Comment :)

XQuery Conditional Expressions
XQuery的条件表达式

“If-Then-Else” expressions are allowed in XQuery.
XQuery允许使用”If-Then-Else”条件表达式。

Look at the following example:
看下面的例子:

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表达式允许有圆括号,另外一点就是,使用了“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.
如果表达返回的只是一个值大于10的q属性,上面的表达式才返回真,如果返回的q多于一个,就会出错。

XQuery 添加元素和属性
w3pop.com / 2006-09-21

p.gifXQuery 语法
XQuery 选择和过滤 n.gif

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

View the “books.xml” file in your browser.
通过你的浏览器查看“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列表的类属性

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

XQuery 选择和过滤
w3pop.com / 2006-09-21

p.gifXQuery 添加元素和属性
XQuery 函数 n.gif

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.
如同在前几章里所看到的,我们用路径表达或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
    • for -(可选的)给从表达式内返回每一个项绑定一个变量。
  • let – (optional)
    let -(可选的)
  • where – (optional) specifies a criteria
    where -(可选的)指定了一个标准
  • order by – (optional) specifies the sort-order of the result
    • order by—(可选的)指定了结果的排列次序
  • return – specifies what to return in the result
    • 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子句会引起重复。在同样的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子句中允许出现一项或多项,用逗号在表达式中把每项分开。

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子句用于指定结果的排列次序。这里我们想用种类和标题来排列结果

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>

XQuery 函数
w3pop.com / 2006-09-21

p.gifXQuery 选择和过滤
XQuery 摘要 n.gif

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:.
函数名称空间(namespaces)默认的前缀是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:是名称空间(namespaces)的默认前缀,所以这个函数的名称并不需要以前缀名调用。

The reference of all the built-in XQuery 1.0 functions is located in our XPath tutorial.
XQuery 1.0内置函数参数参考我们的XPath教程。


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.
如果你找不到你需要用到的函数,你可以自己书写。

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>

XQuery 摘要
w3pop.com / 2006-09-21

p.gifXQuery 函数
XQuery 参考 n.gif

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的更多内容。


Now You Know XQuery, What’s Next?
明白了XQuery,接着学什么呢?

The next step is to learn about XLink and XPointer.
下步是了解有关XLink 和XPointer.的内容

XLink and 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 our XLink and XPointer tutorial.
在我们的XLink and XPointer教程.上,你可以了解关于XLink 和 XPointer的更多内容。

XQuery 参考
w3pop.com / 2006-09-21

p.gifXQuery 摘要
n.gif

XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.
XQuery 1.0 和 XPath 2.0拥有同样的数据模式并支持相同的函数和操作。


XQuery Functions
XQuery函数

XQuery is built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.
XQuery是建立在XPath表达式的基础上的。XQuery 1.0和XPath 2.0拥有相同的数据模式并支持相同的函数和操作

XPath Operators
XPath 操作

XPath Functions
XPath函数

XQuery Data Types
XQuery数据类型

XQuery shares the same data types as XML Schema 1.0 (XSD).
XQuery和XML Schema 1.0 (XSD)拥有相同的数据类型

XSD String
XSD 字符串

XSD Date
XSD日期

XSD Numeric
XSD 数字

XSD Misc
XSD混合数据类型

转载于:https://www.cnblogs.com/xujiaci/archive/2007/08/30/875927.html

今天的文章Xquery教程分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7112.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注