XQuery/XPath 示例
外观
< XQuery
您希望在 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 “驱动程序”。要使用它,只需替换 return 后面的函数即可。
xquery version "1.0";
let $books := doc('/db/apps/training/data/books.xml')/*
return count($books//book)
有几种方法可以测试 XPath 表达式。其中最有用的一种是在 eXist 中的文档中放置 XML 测试数据,然后使用 oXygen 等工具在服务器上执行测试,然后在 oXygen 结果窗口中显示结果。这可以通过在 oXygen 中设置 eXist(而不是默认的内部 Saxon)作为您的“转换方案”来实现。
以下是使用 oXygen IDE 查看这些结果时的屏幕截图
返回整个数据文件
$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()
- node()
- text()
- *
- string(..)
- data(..)
- child:
- parent:
- following-sibling:
- preceding-sibling:
- descendant:
- descendant-or-self