XQuery - FLWOR



FLWOR 是“For, Let, Where, Order by, Return”的縮寫。下表顯示了 FLWOR 表示式中它們的含義:

  • F - For - 選擇所有節點的集合。

  • L - Let - 將結果存入 XQuery 變數中。

  • W - Where - 選擇條件指定節點。

  • O - Order by - 根據條件對指定的節點進行排序。

  • R - Return - 返回最終結果。

示例

以下是一個包含一組書籍資訊的示例 XML 文件。我們將使用 FLWOR 表示式來檢索價格大於 30 的書籍的書名。

books.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
   
   <book category="JAVA">
      <title lang="en">Learn Java in 24 Hours</title>
      <author>Robert</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   
   <book category="DOTNET">
      <title lang="en">Learn .Net in 24 hours</title>
      <author>Peter</author>
      <year>2011</year>
      <price>70.50</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XQuery in 24 hours</title>
      <author>Robert</author>
      <author>Peter</author> 
      <year>2013</year>
      <price>50.00</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XPath in 24 hours</title>
      <author>Jay Ban</author>
      <year>2010</year>
      <price>16.50</price>
   </book>
   
</books>

以下 Xquery 文件包含在上述 XML 文件上要執行的查詢表示式。

books.xqy

let $books := (doc("books.xml")/books/book)
return <results>
{
   for $x in $books
   where $x/price>30
   order by $x/price
   return $x/title
}
</results>

結果

<title lang="en">Learn XQuery in 24 hours</title>
<title lang="en">Learn .Net in 24 hours</title>

驗證結果

要驗證結果,請用上述 XQuery 表示式替換 books.xqy(在環境設定一章中給出)的內容並執行 XQueryTester Java 程式。

廣告