#XPath is a language for finding information in XML documents. XPath is used to navigate through elements and attributes in XML documents.
XPath uses path expressions to navigate in XML documents
XPath contains a library of standard functions
XPath is the main element in XSLT
XPath is a W3C standard
XPath uses path expressions to select nodes or node sets in XML documents. These path expressions are very similar to those we see in regular computer file systems.
XPath contains over 100 built-in functions. These functions are used for string values, numeric values, date and time comparisons, node and QName processing, sequence processing, logical values, and more.
In XPath, there are seven types of nodes: elements, attributes, text, namespaces, processing instructions, comments, and documents node (or become the root node).
In XPath, there are seven types of nodes: Elements, attributes, text, namespaces, processing instructions, comments, and document (root) nodes. XML documents are treated as a tree of nodes. The root of the tree is called the document node or root node.
Please look at the following XML document:
<?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>
Examples of nodes in the above XML document:
<bookstore> (文档节点) <author>J K. Rowling</author> (元素节点) lang="en" (属性节点)
The basic value is a node with no parent or child.
Example of basic value:
J K. Rowling "en"
Item is a basic value or node.
Each element and attribute has a parent.
In the following example, the book element is the parent of the title, author, year and price elements:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
The element node can have zero, one or more sub.
In the following example, the title, author, year and price elements are all children of the book element:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
Nodes with the same parent
In the following example, the title, author, year and price elements are all siblings:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
The parent of a node, the parent of the parent, etc.
In the following example, the ancestors of the title element are the book element and the bookstore element:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
The child of a node, the child of the child, etc.
In the following example, the descendants of bookstore are the book, title, author, year and price elements:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
XPath Axes
##XML instance document We will use this XML document in the following example:<?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>
Result | |||
---|---|---|---|
Select all ancestors of the current node ( parent, grandfather, etc.) | |||
Select all ancestors of the current node (parent, grandfather, etc.) and the current node itself | |||
Select all attributes of the current node | |||
Select all child elements of the current node. | |||
Select all descendant elements (children, grandchildren, etc.) of the current node. | |||
Select all descendant elements (children, grandchildren, etc.) of the current node as well as the current node itself. | |||
Select all nodes after the closing tag of the current node in the document. | |||
Select all namespace nodes of the current node | |||
Select the current node parent node. | |||
Select all nodes before the start tag of the current node in the document. | |||
Select all sibling nodes before the current node. | |||
Select the current node. |
例子 | 结果 |
---|---|
child::book | 选取所有属于当前节点的子元素的 book 节点 |
attribute::lang | 选取当前节点的 lang 属性 |
child::* | 选取当前节点的所有子元素 |
attribute::* | 选取当前节点的所有属性 |
child::text() | 选取当前节点的所有文本子节点 |
child::node() | 选取当前节点的所有子节点 |
descendant::book | 选取当前节点的所有 book 后代 |
ancestor::book | 选择当前节点的所有 book 先辈 |
ancestor-or-self::book | 选取当前节点的所有book先辈以及当前节点(假如此节点是book节点的话) |
child::*/child::price | 选取当前节点的所有 price 孙。 |
XPath 运算符
XPath 表达式可返回节点集、字符串、逻辑值以及数字。
下面列出了可用在 XPath 表达式中的运算符:
运算符 | 描述 | 实例 | 返回值 |
---|---|---|---|
| | 计算两个节点集 | //book | //cd | 返回所有带有 book 和 cd 元素的节点集 |
+ | 加法 | 6 + 4 | 10 |
- | 减法 | 6 - 4 | 2 |
* | 乘法 | 6 * 4 | 24 |
p | 除法 | 8 p 4 | 2 |
= | 等于 | price=9.80 | 如果 price 是 9.80,则返回 true。 如果 price 是 9.90,则返回 fasle。 |
!= | 不等于 | price!=9.80 | 如果 price 是 9.90,则返回 true。 如果 price 是 9.80,则返回 fasle。 |
< | 小于 | price<9.80 | 如果 price 是 9.00,则返回 true。 如果 price 是 9.90,则返回 fasle。 |
<= | 小于或等于 | price<=9.80 | 如果 price 是 9.00,则返回 true。 如果 price 是 9.90,则返回 fasle。 |
> | 大于 | price>9.80 | 如果 price 是 9.90,则返回 true。 如果 price 是 9.80,则返回 fasle。 |
>= | 大于或等于 | price>=9.80 | 如果 price 是 9.90,则返回 true。 如果 price 是 9.70,则返回 fasle。 |
or | 或 | price=9.80 or price=9.70 | 如果 price 是 9.80,则返回 true。 如果 price 是 9.50,则返回 fasle。 |
and | 与 | price>9.00 and price<9.90 | 如果 price 是 9.80,则返回 true。 如果 price 是 8.50,则返回 fasle。 |
mod | 计算除法的余数 | 5 mod 2 | 1 |
我们将在下面的例子中使用这个 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(路径表达式)
下面的这个例子选取了 bookstore 元素下所有的 book 节点:
xmlDoc.selectNodes("/bookstore/book")
下面的例子仅选取 bookstore 元素下第一个 book 节点:
xmlDoc.selectNodes("/bookstore/book[0]")
下面的例子从所有的 price 节点选取文本:
xmlDoc.selectNodes("/bookstore/book/price/text()")
下面的例子会选取所有价格高于 35 的 price 节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/price")
下面的例子会选取所有价格高于 35 的 title 节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/title")
以上就是疯狂XML学习笔记(12)------------XPath的内容,更多相关内容请关注PHP中文网(www.php.cn)!