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
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.
XPath使用路径表达式在XML文档中选择节点或节点套,节点的选择通过以下方式
The most useful path expressions are listed below:
| 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元素的所有子节点
|
| /bookstore |
Selects the root element bookstore 选择根节点bookstore
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
注:如果路径以"/"开始,它通常表示一绝对路径
|
| bookstore/book |
Selects all book确良elements that are children of bookstore
选择bookstore所有的book元素
|
| //book |
Selects all book elements no matter where they are in the document
选择所有的book元素,无论它们在文档的什么地方
|
| bookstore//book |
Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
选择所有的bookstore的胄book 元素,不管它们在哪,只要在bookstore元素内
|
| //@lang |
Selects all attributes that are named lang
选择所有的名字为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[0] |
Selects the first book element that is the child of the bookstore element.
选择bookstore元素的第一个book 元素
Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
注: IE5 和更高的版本指明[0]表示第一个节点,但是根据W3C的标准,应该是 [1]表示第一个节点
|
| /bookstore/book[last()] |
Selects the last book element that is the child of the bookstore element
选择bookstore元素的最后一个book 元素
|
| /bookstore/book[last()-1] |
Selects the last but one book element that is the child of the bookstore element
选择bookstore元素的倒数第二个book 元素
|
| /bookstore/book[position()<3] |
Selects the first two book elements that are children of the bookstore element
选择bookstore元素的前两个book 元素
|
| //title[@lang] |
Selects all the title elements that have an attribute named lang
选择所有具有lang属性的title元素
|
| //title[@lang='eng'] |
Selects all the title elements that have an attribute named lang with a value of 'eng'
选择所有的,lang属性值为'eng'的 title 元素
|
| /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 元素,且book 元素的price 元素的值大于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
选择所有的bookstore元素的 book 的 title元素,且book 元素的price 元素的值大于35.00
|
Selecting Unknown Nodes
XPath wildcards can be used to select unknown XML elements.
| 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
选择所有的bookstore元素的子节点
|
| //* |
Selects all elements in the document
选择文档中所有的元素
|
| //title[@*] |
Selects all title elements which have any attribute
选择的有属性的title元素
|
Selecting Several Paths选择几条路径
By using the | operator in an XPath expression you can select several paths.
在XPath表达式中通过使用"|"操作符可以来选择几条路径
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
选择book元素的所有title 和price 元素
|
| //title | //price |
Selects all the title AND price elements in the document
选择文档中所有的title 和price 元素
|
| /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
选择bookstore元素的book元素的所有title和
文档中所有的price 元素
|