什麼是XPath?

 

Xpath 是一種能夠在XML文檔中尋找信息的語言。它通過XML文檔中的元素和屬性來進行導航。
XPath 是針對XML文檔部分內容定義的語法
XPath 使用路徑表達式在XML文檔中導航
XPath 包含了一系列標準函數
XPath 在XSLT當中是一個主要的元素
XPath 是W3C標準

XPath 路徑表達式:
XPath 使用路徑表達式來選擇XML文檔中的nodes(節)或是node-set(節集)。這些路徑表達式看上去與你平時所見的傳統計算機文件系統路徑非常地相似。

XPath 標準函數:
XPath包含了數量超過100的內置函數。這些函數針對字符串值,數字值,日期和時間比較,節操作,順序操作,布爾值,等等。

XPath被用在XSLT:
在XSLT標準中XPath是主要的元素。沒有XPath知識你將很難建立XSLT文檔。
XQuery和XPointer都建立於XPath表達式。XQuery 1.0 和 XPath 2.0共享相同的數據模型並支持相同的函數和操作。

XPath是W3C標準:
XPath於1999年11月16日成爲W3C的推薦標準。
XPath被設計成爲用語XSLT,XPoniter以及其他XML解析的軟件。

 

二、XPath Nodes(節)

 在XPath中有七種nodes(節):元素,屬性,文字,命名空間,處理說明,註釋,和文檔(根)節。

XPath術語:
Nodes/節:XML文檔被視爲數狀的節。樹的根部被稱爲文檔的節(或根節)。
Atomic values/原子值:原子值是那些沒有子或父的節(無上下關係)。
Items/項目:項目是原子值或節。

Relationship of Nodes(節之間的關係):
Parent/父
Children/子
Siblings/兄
Ancestors/祖:節的父,父的父....都爲祖
Descendants/孫:節的子,子的子...都爲孫

 

三、XPath語法

 XPath使用路徑表達式來選擇XML文檔的節或是節集。順着路徑或步驟來選擇節。

舉例中我們將使用下面的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(選擇節):

一些非常有用的路徑表達式:

表達式 描述
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[選擇屬性]

(實例)下面我們所列舉的表格有路徑表達式以及其結果:

 

路徑表達式 結果
bookstore Selects all the child nodes of the bookstore element[選擇所有bookstore元素的子節]
/bookstore Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

[選擇了bookstore的根元素。注意:如果路徑的開始爲(/)那此路徑一定是到該元素的絕對路徑]
bookstore/book Selects all book elements that are children of bookstore[選擇了所有在bookstore的子元素book元素所包含的所有元素(其實就爲bookstore裏book元素所包含的元素)]
//book Selects all book elements no matter where they are in the document[選擇所有爲book元素的內容而不管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元素的元素內容(只要book元素的祖元素爲bookstore元素那都符合條件)]
//@lang Selects all attributes that are named lang[選擇所有屬性名爲lang的屬性]

Predicates(謂語):

謂語用來指定明確的節所含有的特殊的值。
謂語被嵌入在中括號。

(實例)下面的表格列舉了一些使用了謂語的路徑表達式以及其產生的結果:

 

路徑表達式 結果
/bookstore/book[1] Selects the first book element that is the child of the bookstore element[選擇了bookstore裏的第一個book元素]
/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元素裏price元素內容大於35.00的book元素]
/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,並且其兄弟元素price的內容得大於35.00]

Selecting Unknown Nodes(選擇未知的節):

XPath的通配符可以用來選擇未知的XML元素

 

通配符 描述
* Matches any element node[相吻合的所有元素節]
@* Matches any attribute node[相吻合的所有屬性節]
node() Matches any node of any kind[吻合任何類型的節]
(實例)下面的表格我們將列舉一些路徑表達式以及它們的結果 :

 

路徑表達式 結果
/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(選擇數個路徑):

通過在XPath中使用 | 你可以選擇數個路徑: (實例)下面的表格我們會列舉一些路徑表達式以及其結果:

 

路徑表達 結果
//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[選擇所有book裏的title元素和所有price元素]

四、XPath軸

舉例中我們將使用下面的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>

XPath Axes(XPath軸):

軸定義了相對於當前節的節集。

軸名 結果
ancestor Selects all ancestors (parent, grandparent, etc.) of the current node[選擇了當前節的所有祖(父,祖父,等等)]
ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself[選擇當前節的所有祖並且還有當前節自己]
attribute Selects all attributes of the current node[選擇所有當前節的屬性]
child Selects all children of the current node[選擇所有當前節的子]
descendant Selects all descendants (children, grandchildren, etc.) of the current node[選擇所有當前節的孫(子,孫子,等等)]
descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself[選擇當前節的所有孫以及它本身]
following Selects everything in the document after the closing tag of the current node[選擇所有在關閉當前節標籤後的所有內容]
following-sibling Selects all siblings after the current node[選擇所有當前節後的兄]
namespace Selects all namespace nodes of the current node[選擇所有當前節的命名空間]
parent Selects the parent of the current node[選擇當前節的父]
preceding Selects everything in the document that is before the start tag of the current node[選擇當前節之前的所有內容]
preceding-sibling Selects all siblings before the current node[選擇所有當前節之前的兄]
self Selects the current node[選擇當前節]

Location Path Expression(路徑表達試定位):

定位路徑可以是絕對的也可以是相對的。

絕對定位的路徑由(/)開始,而相對定位就不這樣。定位的路徑由一個或多個步驟所組成,每部分由(/)相分隔:

An absolute location path:
/step/step/...
A relative location path:
step/step/...

Each step is evaluated against the nodes in the current node-set.
在當前的節集中每步的賦值是逆向的

A step consists of:

  • an axis (defines the tree-relationship between the selected nodes and the current node)
  • a node-test (identifies a node within an axis)[在軸中鑑定節]
  • zero or more predicates (to further refine the selected node-set)[0個或多個謂語可以來更好的選擇節]

定位的語法:

    axisname::nodetest[predicate]
(實例) :
Example 結果
child::book Selects all book nodes that are children of the current node[選擇當前節點下所有爲book的子節點]
attribute::lang Selects the lang attribute of the current node[選擇當前節點下所有屬性爲lang的內容]
child::* Selects all children of the current node[選擇當前節下所有的子節]
attribute::* Selects all attributes of the current node[選擇當前節所有的屬性]
child::text() Selects all text child nodes of the current node[選擇當前節點所有子節點的文字]
child::node() Selects all child nodes of the current node[選擇所有當前節點的子節點]
descendant::book Selects all book descendants of the current node[選擇當前節點所有爲book的孫節點]
ancestor::book Selects all book ancestors of the current node[選擇所有當前祖節點爲book的節點]
ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node[當前節點和其祖節點爲book的節點]
child::*/child::price Selects all price grandchildren of the current node[當前節點所有含price的孫子節點]

五、XPath運算符

Xpaht表達式的返回值是:node-set、字符串、boolean、數值。

Xpath裏使用的運算符:

 

Operator Description Example Return value
| Computes two node-sets //book | //cd Returns a node-set with all book and cd elements
+ Addition 6 + 4 10
- Subtraction 6 - 4 2
* Multiplication

6 * 4

24
div Division 8 div 4 2
= Equal price=9.80 true if price is 9.80
false if price is 9.90
!= Not equal price!=9.80 true if price is 9.90
false if price is 9.80
< Less than price<9.80 true if price is 9.00
false if price is 9.80
<= Less than or equal to price<=9.80 true if price is 9.00
false if price is 9.90
> Greater than price>9.80 true if price is 9.90
false if price is 9.80
>= Greater than or equal to price>=9.80 true if price is 9.90
false if price is 9.70
or or price=9.80 or price=9.70 true if price is 9.80
false if price is 9.50
and and price>9.00 and price<9.90 true if price is 9.80
false if price is 8.50
mod Modulus (division remainder) 5 mod 2 1

六、精彩實例

讓我們來嘗試通過觀察一些實例來學習基礎的XPath語法。

我們將使用下面這個XML文檔來進行實例:
"books.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>

 

選擇節點:

我們使用了XMLDOM對象來加載XML文檔並用selectNode()函數來進行XML文檔上節點的選擇:

var xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async
="false"
xmlDoc.load(
"books.xml")

xmlDoc.selectNodes(path expression)

選擇所有book節點:

下面這個實例就會選擇所有bookstore元素以下的book節點:

xmlDoc.selectNodes("/bookstore/book")

選擇第一個book節點:

xmlDoc.selectNodes("/bookstore/book[0]")

選擇prices:

xmlDoc.selectNodes("/bookstore/book/price/text()")

選擇price大於35的price節點:

xmlDoc.selectNodes("/bookstore/book[price>35]/price")

選擇Price大於35的title節點:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章