疯狂XML学习笔记(12)------------XPath
XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。 什么是 XPath?
XPath 路径表达式XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似。 XPath 标准函数XPath 含有超过 100 个内建的函数。这些函数用于字符串值、数值,日期和时间比较、节点和 QName 处理、序列处理、逻辑值等等。 在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或成为根节点)。 XPath 术语节点(Node)在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。树的根被称为文档节点或者根节点。 请看下面这个 XML 文档: <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> 上面的XML文档中的节点例子: <bookstore> (文档节点) <author>J K. Rowling</author> (元素节点) lang="en" (属性节点) 基本值(或称原子值,Atomic value)基本值是无父或无子的节点。 基本值的例子: J K. Rowling "en" 项目(Item)项目是基本值或者节点。 节点关系父(Parent)每个元素以及属性都有一个父。 在下面的例子中,book 元素是 title、author、year 以及 price 元素的父: <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> 子(Children)元素节点可有零个、一个或多个子。 在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子: <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> 同胞(Sibling)拥有相同的父的节点 在下面的例子中,title、author、year 以及 price 元素都是同胞: <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> 先辈(Ancestor)某节点的父、父的父,等等。 在下面的例子中,title 元素的先辈是 book 元素和 bookstore 元素: <bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> 后代(Descendant)某个节点的子,子的子,等等。 在下面的例子中,bookstore的后代是 book、title、author、year 以及 price 元素: <bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
XPath Axes(坐标轴) XML 实例文档我们将在下面的例子中使用此 XML 文档: <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore> XPath 轴轴可定义某个相对于当前节点的节点集。
位置路径表达式位置路径可以是绝对的,也可以是相对的。 绝对路径起始于正斜杠( / ),而相对路径不会这样。在两种情况中,位置路径均包括一个或多个步,每个步均被斜杠分割: 绝对位置路径:/step/step/... 相对位置路径:step/step/... 每个步均根据当前节点集之中的节点来进行计算。 步(step)包括:
步的语法:轴名称::节点测试[谓语] 实例
XPath 运算符
XPath 表达式可返回节点集、字符串、逻辑值以及数字。 XPath 运算符下面列出了可用在 XPath 表达式中的运算符:
XML实例文档我们将在下面的例子中使用这个 XML 文档: "books.xml" :<?xml version="1.0" encoding="ISO-8859-1"?> <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> 节点选取我们将使用微软的 XML DOM 对象来载入 XML 文档,并使用 selectNodes() 函数从 XML 文档选取节点: set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("books.xml") xmlDoc.selectNodes(路径表达式) 选取所有的 book 节点下面的这个例子选取了 bookstore 元素下所有的 book 节点: xmlDoc.selectNodes("/bookstore/book") 选取第一个 book 节点下面的例子仅选取 bookstore 元素下第一个 book 节点: xmlDoc.selectNodes("/bookstore/book[0]") 选取 price下面的例子从所有的 price 节点选取文本: xmlDoc.selectNodes("/bookstore/book/price/text()") 选取价格高于 35 的 price 价格下面的例子会选取所有价格高于 35 的 price 节点: xmlDoc.selectNodes("/bookstore/book[price>35]/price") 选取价格高于 35 的 title 节点下面的例子会选取所有价格高于 35 的 title 节点: xmlDoc.selectNodes("/bookstore/book[price>35]/title") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |