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翻譯 有誤地方 實屬正常 請見諒 呵呵~~