XQuery FLWOR 表达式
XML 实例文档
我们将在下面的例子中继续使用这个 "books.xml" 文档(与上一节中的 XML 文件相同)。
在您的浏览器中查看 "books.xml" 文件。
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!-- 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>
如果使用 FLWOR 从 "books.xml" 选取节点
请看下面这个路径表达式:
doc("books.xml")/bookstore/book[price>30]/title
上面这个表达式可选取 bookstore 元素下的 book 元素下所有的 title 元素,并且其中的 price 元素的值必须大于 30。
下面这个 FLWOR 表达式所选取的数据和上面的路径表达式是相同的:
for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title
结果是:
<title lang="en">XQuery Kick Start</title> <title lang="en">Learning XML</title>
通过 FLWOR,您可以对结果进行排序:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
FLWOR 是 "For, Let, Where, Order by, Return" 的只取首字母缩写。
for 语句把 bookstore 元素下的所有 book 元素提取到名为 $x 的变量中。
where 语句选取了 price 元素值大于 30 的 book 元素。
order by 语句定义了排序次序。将根据 title 元素进行排序。
return 语句规定返回什么内容。在此返回的是 title 元素。
上面的 XQuery 表达式的结果:
<title lang="en">Learning XML</title> <title lang="en">XQuery Kick Start</title>
发表评论 取消回复