xpath簡介__用於在XML中查找

XPath介紹

XPath is a language for finding information in an XML document. XPath is used to navigate through elements and attributes in an XML document.
Xpath是一種能夠在XML文檔中尋找信息的語言。它通過XML文檔中的元素和屬性來進行導航。 


--------------------------------------------------------------------------------

What You Should Already Know
你應具備的知識
Before you continue you should have a basic understanding of the following:
在你繼續前你應該具備以下知識的基礎:

HTML / XHTML 
XML / XML Namespaces[命名空間] 

--------------------------------------------------------------------------------

What is XPath?
什麼是XPath?
XPath is a syntax for defining parts of an XML document
XPath 是針對XML文檔部分內容定義的語法 
XPath uses path expressions to navigate in XML documents
XPath 使用路徑表達式在XML文檔中導航 
XPath contains a library of standard functions
XPath 包含了一系列標準函數 
XPath is a major element in XSLT
XPath 在XSLT當中是一個主要的元素 
XPath is a W3C Standard
XPath是W3C標準 

--------------------------------------------------------------------------------

XPath Path Expressions
XPath 路徑表達式
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.
XPath 使用路徑表達式來選擇XML文檔中的nodes(節)或是node-set(節集)。這些路徑表達式看上去與你平時所見的傳統計算機文件系統路徑非常地相似。


--------------------------------------------------------------------------------

XPath Standard Functions
XPath 標準函數
XPath includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
XPath包含了數量超過100的內置函數。這些函數針對字符串值,數字值,日期和時間比較,節操作,順序操作,布爾值,等等。 


--------------------------------------------------------------------------------

XPath is Used in XSLT
XPath被用在XSLT
XPath is a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
在XSLT標準中XPath是主要的元素。沒有XPath知識你將很難建立XSLT文檔。

XQuery and XPointer are both built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. 
XQuery和XPointer都建立於XPath表達式。XQuery 1.0 和 XPath 2.0共享相同的數據模型並支持相同的函數和操作


--------------------------------------------------------------------------------

XPath is a W3C Standard
XPath是W3C標準
XPath became a W3C Recommendation 16. November 1999.
XPat於1999年11月16日成爲W3C的推薦標準

XPath was designed to be used by XSLT, XPointer and other XML parsing software.
XPath被設計成爲用語XSLT,XPoniter以及其他XML解析的軟件。

 

 

XPath Nodes(節)

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在XPath中有七種nodes(節):元素,屬性,文字,命名空間,處理說明,註釋,和文檔(根)節。


--------------------------------------------------------------------------------

XPath Terminology
XPath術語
Nodes/節
In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).
XML文檔被視爲數狀的節。樹的根部被稱爲文檔的節(或根節)。

Look at the following XML document:
觀察下面的XML文檔:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author> 
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>
Example of nodes in the XML document above:
上面舉例的XML文檔的節有:

<bookstore>  (document node)
<author>J K. Rowling</author>  (element node)
lang="en"  (attribute node)
Atomic values
原子值
Atomic values are nodes with no children or parent.
原子值是那些沒有子或父的節(無上下關係)。

Example of atomic values:
舉例中的原子值:

J K. Rowling
"en"
Items
項目
Items are atomic values or nodes.
項目是原子值或節。


--------------------------------------------------------------------------------

Relationship of Nodes
節之間的關係
Parent/父
Each element and attribute has one parent.
每個元素和屬性有一父親。

In the following example; the book element is the parent of the title, author, year, and price:
下面的舉例中:book元素是title,author,year和price的父親

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Children/子
Element nodes may have zero, one or more children.
元素節可能有0個或多個子

In the following example; the title, author, year, and price elements are all children of the book element:
下面的舉例中:title,author,year和price元素都是book元素的子元素

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Siblings/兄
Nodes that have the same parent.
指那些有相同父的

In the following example; the title, author, year, and price elements are all siblings:
下面的舉例中title, author, year, 和 price元素都爲兄弟

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Ancestors/祖
A node's parent, parent's parent, etc.
節的父,父的父....都爲祖

In the following example; the ancestors of the title element are the book element and the bookstore element:
下面的舉例中:book元素和bookstore元素都爲title元素的祖元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>
Descendants/孫
A node's children, children's children, etc.
節的子,子的子...都爲孫

In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
下面的舉例中:bookstore元素的孫有book,title,author,year以及price元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>
XPath語法

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:
一些非常有用的路徑表達式: 

表達式 描述 
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:
下面我們所列舉的表格有路徑表達式以及其結果:

路徑表達式 結果 
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
謂語
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:
下面的表格列舉了一些使用了謂語的路徑表達式以及其產生的結果:

路徑表達式 結果 
/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 wildcards can be used to select unknown XML elements.
XPath的通配符可以用來選擇未知的XML元素

通配符 描述 
* 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:
下面的表格我們將列舉一些路徑表達式以及它們的結果

路徑表達式 結果 
/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並且其含有屬性] 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章