XPath 简单语法
XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath 通过路径表达式从XML文档中选取节点或节点集。该节点是通过其后的一条语句或相应的步骤选取的。
The XML Example Document
XML 案例文档
We will use the following XML document in the examples below.
我们将在接下来的案例中引用下面这个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> |
Selecting Nodes
选取节点
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:
XPath使用路径表达式在XML文档中选取节点。该节点是通过其后的一条语句或相应的步骤选取的。下面列出了最常用的路径表达式:
Expression |
Description |
nodename |
Selects all child nodes of the node |
/ |
Selects from the root node |
// |
Selects nodes in the document from the current node that match the selection no matter where they are |
. |
Selects the current node |
.. |
Selects the parent of the current node |
@ |
Selects attributes |
Examples
案例
In the table below we have listed some path expressions and the result of the expressions:
在下述表格中,我们罗列了一些路径表达式及其运行的结果:
Path Expression 表达式 |
Result |
bookstore |
Selects all the child nodes of the bookstore element |
/bookstore |
Selects the root element bookstore |
bookstore/book |
Selects all book elements that are children of bookstore |
//book |
Selects all book elements no matter where they are in the document |
bookstore//book |
Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element |
//@lang |
Selects all attributes that are named lang |
Predicates
条件
Predicates are used to find a specific node or a node that contains a specific value.
它指定了选取节点的范围。
Predicates are always embedded in square brackets.
通常使用方括号[ ]来指定条件。
Examples
实例
In the table below we have listed some path expressions with predicates and the result of the expressions:
在下述表格中,我们列举了一些路径表达式及其运行结果:
Path Expression 表达式 |
Result |
/bookstore/book[1] |
Selects the first book element that is the child of the bookstore element |
/bookstore/book[last()] |
Selects the last book element that is the child of the bookstore element |
/bookstore/book[last()-1] |
Selects the last but one book element that is the child of the bookstore element |
/bookstore/book[position()<3] |
Selects the first two book elements that are children of the bookstore element |
//title[@lang] |
Selects all the title elements that have an attribute named lang |
//title[@lang='eng'] |
Selects all the title elements that have an attribute named lang with a value of 'eng' |
/bookstore/book[price>35.00] |
Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 |
/bookstore/book[price>35.00]/title |
Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 |
Selecting Unknown Nodes
选取未知节点
XPath wildcards can be used to select unknown XML elements.
可以通过 XPath 通配符选取未知的XML元素。
Wildcard 通配符 |
Description |
* |
Matches any element node |
@* |
Matches any attribute node |
node() |
Matches any node of any kind |
Examples
案例
In the table below we have listed some path expressions and the result of the expressions:
在下述表格中,我们列出了一些路径表达式及其运行结果:
Path Expression 表达式 |
Result |
/bookstore/* |
Selects all the child nodes of the bookstore element |
//* |
Selects all elements in the document |
//title[@*] |
Selects all title elements which have any attribute |
Selecting Several Paths
选取多个路径
By using the | operator in an XPath expression you can select several paths.
你可以同过在表达式中添加“ | ”操作符来选取多个路径。
Examples
案例
In the table below we have listed some path expressions and the result of the expressions:
在下述表格中,我们列举了一些路径表达式及其运行结果:
Path Expression 表达式 |
Result |
//book/title | //book/price |
Selects all the title AND price elements of all book elements |
//title | //price |
Selects all the title AND price elements in the document |
/bookstore/book/title | //price |
Selects all the title elements of the book element of the bookstore element AND all the price elements in the document |