当前位置: 首页 > 网络学院 > 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   浏览: 614 ::
收藏到网摘: n/a

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>

评论 (0) All

登陆 | 还没注册?