XPath使用路径表达式来选择节点或一个XML文档节点集。被选中的节点通过以下路径或步骤。
XML实例文档
我们将在下面的例子使用下面的XML文档。
选择节点
XPath使用路径表达式来选择XML文档中的节点。被选中的节点通过以下路径或步骤。最有用的路径表达式如下:
Expression表达式 | Description描述 |
---|---|
nodename | Selects all child nodes of the named 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 选择属性 |
在表格下面我们列出了一些路径表达式以及表达式的结果:
路径表达式 | 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 选择所有book元素属于children of bookstore |
//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 选择所有book元素属于bookstore元素的后代,无论身在何处,他们都是bookstore element元素 |
//@lang | Selects all attributes that are named lang 选取所有名为lang属性 |
Predicates谓语用来查找特定节点或一个节点,其中包含一个特定值。
Predicates 被嵌入在方括号内。
在表格下面我们列出了一些路径表达式和谓词的表达式的结果:
Path Expression | Result |
---|---|
/bookstore/book[1] | Selects the first book element that is the child of the bookstore element. 选择第一个book元素,它是child of the bookstore 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 选择最后一个book元素,它是元素的子书店 |
/bookstore/book[last()-1] | Selects the last but one book element that is the child of the bookstore element 选择最后一个book元素,但是这是书店的子元素 |
/bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element 选择第2个书元素的儿童元素 |
//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' 选择所有的标题元素有一个属性名为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 选择所有的书店元素有一个更大的值比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 选择所有的书店元素有一个更大的值比35.00元的书价元素名称元素 |
未知节点选择
XPath的通配符可以用来选择未知的XML元素。
Wildcard | Description |
---|---|
* | Matches any element node 匹配任何元素节点 |
@* | Matches any attribute node 匹配任何属性节点 |
node() | Matches any node of any kind 匹配任何形式的节点 |
在表格下面我们列出了一些路径表达式以及表达式的结果:
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 选择所有标题内容有任何属性 |
选择几种途径
通过使用XPath表达式的|你可以选择几个路径。
在表格下面我们列出了一些路径表达式以及表达式的结果:
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 选择所有的书店元素book元素名称元素和所有文件中的价格因素 |
英文网址:http://www.w3schools.com/xpath/xpath_syntax.asp
Google翻译 有误地方 实属正常 请见谅 呵呵~~