跳轉到內容

XQuery/XPath 示例

來自華夏公益教科書,開放書籍,開放世界

您希望在 XML 文件中選擇特定的結構。您希望使用一種在所有 W3C XML 標準中一致的語言。這種語言就是 XPath。

示例輸入檔案

[編輯 | 編輯原始碼]

將此檔案放到 /db/apps/training/data/books.xml

<books>
    <description>A list of books useful for people first learning how to build web XML web applications.</description>
    <book>
        <title>XQuery</title>
        <author>Priscilla Walmsley</author>
        <description>This book is a highly detailed, through and complete tour of the W3C Query language.  It covers all the
        key aspects of the language as well as</description>
        <format>Trade press</format>
        <license>Commercial</license>
        <list-price>49.95</list-price>
    </book>
    <book>
        <title>XQuery Examples</title>
        <author>Chris Wallace</author>
        <author>Dan McCreary</author>
        <description>This book provides a variety of XQuery example programs and is designed to work with the eXist open-source native XML application server.</description>
        <format>Wiki-books</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
    <book>
        <title>XForms Tutorial and Cookbook</title>
        <author>Dan McCreary</author>
        <description>This book is an excellent guide for anyone that is just beginning to learn the XForms standard.  The book
        is focused on providing the reader with simple, but complete examples of how to create XForms web applications.</description>
        <format>Wikibook</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
    <book>
        <title>XRX: XForms, Rest and XQuery</title>
        <author>Dan McCreary</author>
        <description>This book is an overview of the key architectural and design patters.</description>
        <format>Wikibook</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
    <book>
        <title>Making Sense of NoSQL</title>
        <author>Dan McCreary</author>
        <author>Ann Kelly</author>
        <description>A guide to NoSQL for managers and solution architects.  Presents six NoSQL architectures and when to use each architecture.</description>
        <format>Trade Press</format>
        <license>Commercial</license>
        <list-price>32.00</list-price>
    </book>
</books>

XPath 提供了許多函式和軸來遍歷 XML 結構。

示例測試 XQuery 指令碼

[編輯 | 編輯原始碼]

這是一個用於這些測試的示例 XQuery “驅動程式”。要使用它,只需替換 return 後面的函式即可。

xquery version "1.0";
let $books := doc('/db/apps/training/data/books.xml')/*

return count($books//book)

oXygen 中 XQuery 結果的螢幕截圖

[編輯 | 編輯原始碼]

有幾種方法可以測試 XPath 表示式。其中最有用的一種是在 eXist 中的文件中放置 XML 測試資料,然後使用 oXygen 等工具在伺服器上執行測試,然後在 oXygen 結果視窗中顯示結果。這可以透過在 oXygen 中設定 eXist(而不是預設的內部 Saxon)作為您的“轉換方案”來實現。

以下是使用 oXygen IDE 檢視這些結果時的螢幕截圖

screen image of XQuery execution in oXygen IDE

示例 XPath 表示式

[編輯 | 編輯原始碼]

返回整個資料檔案

  $books

僅獲取書籍及其所有資料。

  $books//book

獲取所有書籍標題

  $books//title

獲取收藏集描述

  $books/description/text()

獲取所有書籍的描述

  $books//book/description/text()

計數和數學運算

[編輯 | 編輯原始碼]

使用絕對路徑計算書籍數量

  count($books/book)  (: should return 5 :)

使用 // 計算書籍數量。在 eXist 中,對於大型收藏,執行速度要快得多。

  count($books//book) (: should return 5 :)

獲取書籍收藏中所有標題的序列。

  $books//title/text()

計算收藏中所有書籍的總價和平均價。

  sum($books//list-price/text())  (: Should return a number such as 171.8 :)
  avg($books//list-price/text())  (: Should return a number such as 34.36 :)
  min($books//list-price/text())  (: Should return a number such as 29.95 :)
  max($books//list-price1/text())  (: Should return a number such as 49.95 :)

以下指令碼展示了一些這些函式和軸的使用方式。

新增謂詞

[編輯 | 編輯原始碼]

謂詞是在 XPath 表示式末尾新增的限定符。它通常用於從結果集中篩選節點。謂詞類似於 SQL 中的 WHERE 子句。

獲取所有華夏公益教科書

  $books//book[format='Wikibook']

僅獲取華夏公益教科書的標題

  $books//book[format='Wikibook']/title/text()

獲取標題中包含“XQuery”的書籍

  $books//book[contains(title, 'XQuery')]/title/text()

複雜謂詞

[編輯 | 編輯原始碼]
  1. node()
  2. text()
  3. *
  4. string(..)
  5. data(..)
  6. child:
  7. parent:
  8. following-sibling:
  9. preceding-sibling:
  10. descendant:
  11. descendant-or-self

使用不同標籤遍歷樹

使用單個標籤遍歷樹

華夏公益教科書