XPath 簡單文法

來源:互聯網
上載者:User

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元素的所有子節點

/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元素

//@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[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元素下所有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元素下所有price元素值大於35.00的book節點下的title元素

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
選取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.
你可以同過在運算式中添加“ | ”操作符來選取多個路徑。
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元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.