当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery 实例
Let's try to learn some basic XQuery syntax by looking at an example.
让我们通过下面这个案例来学习一些关于XQuery 的基本语法。
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"> <book category="CHILDREN"> <book category="WEB"> <book category="WEB"> </bookstore> |
View the "books.xml" file in your browser.
在浏览器中浏览“books.xml”文件。
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") |
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> |
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"> |