泰仔在線

          java學(xué)習(xí),心情日記,繽紛時(shí)刻
          posts - 100, comments - 34, trackbacks - 0, articles - 0

          (本文原址:http://blog.csdn.net/linqingfeng/archive/2006/05/02/705230.aspx)
          Xpath介紹
          翻譯:Linyupark / 2006-03-24

          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文檔中尋找信息的語(yǔ)言。它通過(guò)XML文檔中的元素和屬性來(lái)進(jìn)行導(dǎo)航。


          What You Should Already Know
          你應(yīng)具備的知識(shí)

          Before you continue you should have a basic understanding of the following:
          在你繼續(xù)前你應(yīng)該具備以下知識(shí)的基礎(chǔ):

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

          What is XPath?
          什么是XPath?

          • XPath is a syntax for defining parts of an XML document
            XPath 是針對(duì)XML文檔部分內(nèi)容定義的語(yǔ)法
          • XPath uses path expressions to navigate in XML documents
            XPath 使用路徑表達(dá)式在XML文檔中導(dǎo)航
          • XPath contains a library of standard functions
            XPath 包含了一系列標(biāo)準(zhǔn)函數(shù)
          • XPath is a major element in XSLT
            XPath 在XSLT當(dāng)中是一個(gè)主要的元素
          • XPath is a W3C Standard
            XPath是W3C標(biāo)準(zhǔn)

          XPath Path Expressions
          XPath 路徑表達(dá)式

          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 使用路徑表達(dá)式來(lái)選擇XML文檔中的nodes(節(jié))或是node-set(節(jié)集)。這些路徑表達(dá)式看上去與你平時(shí)所見(jiàn)的傳統(tǒng)計(jì)算機(jī)文件系統(tǒng)路徑非常地相似。


          XPath Standard Functions
          XPath 標(biāo)準(zhǔn)函數(shù)

          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包含了數(shù)量超過(guò)100的內(nèi)置函數(shù)。這些函數(shù)針對(duì)字符串值,數(shù)字值,日期和時(shí)間比較,節(jié)操作,順序操作,布爾值,等等。


          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標(biāo)準(zhǔn)中XPath是主要的元素。沒(méi)有XPath知識(shí)你將很難建立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表達(dá)式。XQuery 1.0 和 XPath 2.0共享相同的數(shù)據(jù)模型并支持相同的函數(shù)和操作


          XPath is a W3C Standard
          XPath是W3C標(biāo)準(zhǔn)

          XPath became a W3C Recommendation 16. November 1999.
          XPat于1999年11月16日成為W3C的推薦標(biāo)準(zhǔn)

          XPath was designed to be used by XSLT, XPointer and other XML parsing software.
          XPath被設(shè)計(jì)成為用語(yǔ)XSLT,XPoniter以及其他XML解析的軟件。

          XPath Nodes(節(jié))
          翻譯:Linyupark / 2006-03-24

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


          XPath Terminology
          XPath術(shù)語(yǔ)

          Nodes/節(jié)

          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文檔被視為數(shù)狀的節(jié)。樹(shù)的根部被稱為文檔的節(jié)(或根節(jié))。

          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文檔的節(jié)有:

          <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.
          原子值是那些沒(méi)有子或父的節(jié)(無(wú)上下關(guān)系)。

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

          J K. Rowling
          "en"

          Items
          項(xiàng)目

          Items are atomic values or nodes.
          項(xiàng)目是原子值或節(jié)。


          Relationship of Nodes
          節(jié)之間的關(guān)系

          Parent/父

          Each element and attribute has one parent.
          每個(gè)元素和屬性有一父親。

          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.
          元素節(jié)可能有0個(gè)或多個(gè)子

          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.
          節(jié)的父,父的父....都為祖

          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.
          節(jié)的子,子的子...都為孫

          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語(yǔ)法
          翻譯:Linyupark / 2006-03-24

          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使用路徑表達(dá)式來(lái)選擇XML文檔的節(jié)或是節(jié)集。順著路徑或步驟來(lái)選擇節(jié)。


          The XML Example Document
          XML實(shí)例文檔

          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
          選擇節(jié)

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

          表達(dá)式 描述
          nodename Selects all child nodes of the node[選擇所有目前節(jié)的子節(jié)]
          / Selects from the root node[從根節(jié)進(jìn)行選擇]
          // Selects nodes in the document from the current node that match the selection no matter where they are [選擇文檔中相吻合的節(jié)而不管其在文檔的何處]
          . Selects the current node[選擇當(dāng)前節(jié)]
          .. Selects the parent of the current node[當(dāng)前節(jié)的父節(jié)]
          @ Selects attributes[選擇屬性]

          Examples
          實(shí)例

          In the table below we have listed some path expressions and the result of the expressions:
          下面我們所列舉的表格有路徑表達(dá)式以及其結(jié)果:

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

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

          [選擇了bookstore的根元素。注意:如果路徑的開(kāi)始為(/)那此路徑一定是到該元素的絕對(duì)路徑]
          bookstore/book Selects all book elements that are children of bookstore[選擇了所有在bookstore的子元素book元素所包含的所有元素(其實(shí)就為bookstore里book元素所包含的元素)]
          //book Selects all book elements no matter where they are in the document[選擇所有為book元素的內(nèi)容而不管book元素處于何處(有不同的父也沒(méi)關(guān)系)]
          bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素內(nèi)所有含有book元素的元素內(nèi)容(只要book元素的祖元素為bookstore元素那都符合條件)]
          //@lang Selects all attributes that are named lang[選擇所有屬性名為lang的屬性]


          Predicates
          謂語(yǔ)

          Predicates are used to find a specific node or a node that contains a specific value.
          謂語(yǔ)用來(lái)指定明確的節(jié)所含有的特殊的值

          Predicates are always embedded in square brackets.
          謂語(yǔ)被嵌入在中括號(hào)

          Examples
          舉例

          In the table below we have listed some path expressions with predicates and the result of the expressions:
          下面的表格列舉了一些使用了謂語(yǔ)的路徑表達(dá)式以及其產(chǎn)生的結(jié)果:

          路徑表達(dá)式 結(jié)果
          /bookstore/book[1] Selects the first book element that is the child of the bookstore element[選擇了bookstore里的第一個(gè)book元素]
          /bookstore/book[last()] Selects the last book element that is the child of the bookstore element[選擇bookstore里最后一個(gè)book元素]
          /bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element[bookstore中倒數(shù)第二個(gè)book元素]
          /bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element[在bookstore中前兩個(gè)book元素]
          //title[@lang] Selects all the title elements that have an attribute named lang[選擇所有含有l(wèi)ang屬性的title元素]
          //title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'[選擇所有含有l(wèi)ang屬性并且值為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元素內(nèi)容大于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的內(nèi)容得大于35.00]


          Selecting Unknown Nodes
          選擇未知的節(jié)

          XPath wildcards can be used to select unknown XML elements.
          XPath的通配符可以用來(lái)選擇未知的XML元素

          通配符 描述
          * Matches any element node[相吻合的所有元素節(jié)]
          @* Matches any attribute node[相吻合的所有屬性節(jié)]
          node() Matches any node of any kind[吻合任何類型的節(jié)]

          Examples實(shí)例

          In the table below we have listed some path expressions and the result of the expressions:
          下面的表格我們將列舉一些路徑表達(dá)式以及它們的結(jié)果

          路徑表達(dá)式 結(jié)果
          /bookstore/* Selects all the child nodes of the bookstore element[選擇所有bookstore的子節(jié)]
          //* Selects all elements in the document[選擇所有文檔中的元素]
          //title[@*] Selects all title elements which have any attribute[選擇元素為title并且其含有屬性]


          Selecting Several Paths
          選擇數(shù)個(gè)路徑

          By using the | operator in an XPath expression you can select several paths.
          通過(guò)在XPath中使用 | 你可以選擇數(shù)個(gè)路徑

          Examples
          實(shí)例

          In the table below we have listed some path expressions and the result of the expressions:
          下面的表格我們會(huì)列舉一些路徑表達(dá)式以及其結(jié)果:

          路徑表達(dá) 結(jié)果
          //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 軸
          翻譯:Linyupark / 2006-03-24

          The XML Example Document
          XML舉例文檔

          We will use the following XML document in the examples below.
          我么將使用該XML文檔進(jìn)行下面的舉例說(shuō)明

          <?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軸

          An axis defines a node-set relative to the current node.
          軸定義了相對(duì)于當(dāng)前節(jié)的節(jié)集

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


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

          A location path can be absolute or relative.
          定位路徑可以是絕對(duì)的也可以是相對(duì)的

          An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:
          絕對(duì)定位的路徑由(/)開(kāi)始,而相對(duì)定位就不這樣。定位的路徑由一個(gè)或多個(gè)步驟所組成,每部分由(/)相分隔:

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

          Each step is evaluated against the nodes in the current node-set.
          在當(dāng)前的節(jié)集中每步的賦值是逆向的

          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)[在軸中鑒定節(jié)]
          • zero or more predicates (to further refine the selected node-set)[0個(gè)或多個(gè)謂語(yǔ)可以來(lái)更好的選擇節(jié)]

          The syntax for a location step is:
          定位的語(yǔ)法

          axisname::nodetest[predicate]

          Examples
          實(shí)例

          Example 結(jié)果
          child::book Selects all book nodes that are children of the current node[選擇當(dāng)前節(jié)點(diǎn)下所有為book的子節(jié)點(diǎn)]
          attribute::lang Selects the lang attribute of the current node[選擇當(dāng)前節(jié)點(diǎn)下所有屬性為lang的內(nèi)容]
          child::* Selects all children of the current node[選擇當(dāng)前節(jié)下所有的子節(jié)]
          attribute::* Selects all attributes of the current node[選擇當(dāng)前節(jié)所有的屬性]
          child::text() Selects all text child nodes of the current node[選擇當(dāng)前節(jié)點(diǎn)所有子節(jié)點(diǎn)的文字]
          child::node() Selects all child nodes of the current node[選擇所有當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)]
          descendant::book Selects all book descendants of the current node[選擇當(dāng)前節(jié)點(diǎn)所有為book的孫節(jié)點(diǎn)]
          ancestor::book Selects all book ancestors of the current node[選擇所有當(dāng)前祖節(jié)點(diǎn)為book的節(jié)點(diǎn)]
          ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node[當(dāng)前節(jié)點(diǎn)和其祖節(jié)點(diǎn)為book的節(jié)點(diǎn)]
          child::*/child::price Selects all price grandchildren of the current node[當(dāng)前節(jié)點(diǎn)所有含price的孫子節(jié)點(diǎn)]

          XPath運(yùn)算符
          翻譯:無(wú) / 2006-03-24

          An XPath expression returns either a node-set, a string, a Boolean, or a number.


          XPath Operators

          Below is a list of the operators that can be used in XPath expressions:

          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實(shí)例
          翻譯:Linyupark / 2006-03-24

          Let's try to learn some basic XPath syntax by looking at some examples.
          讓我們來(lái)嘗試通過(guò)觀察一些實(shí)例來(lái)學(xué)習(xí)基礎(chǔ)的XPath語(yǔ)法


          The XML Example Document

          We will use the following XML document in the examples below.
          我們將使用下面這個(gè)XML文檔來(lái)進(jìn)行實(shí)例

          "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>

          View the "books.xml" file in your browser.


          Selecting Nodes
          選擇節(jié)點(diǎn)

          We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:
          我們使用了XMLDOM對(duì)象來(lái)加載XML文檔并用selectNode()函數(shù)來(lái)進(jìn)行XML文檔上節(jié)點(diǎn)的選擇:

          set xmlDoc=CreateObject("Microsoft.XMLDOM")
          xmlDoc.async="false"
          xmlDoc.load("books.xml")
          xmlDoc.selectNodes(path expression)


          Select all book Nodes
          選擇所有book節(jié)點(diǎn)

          The following example selects all the book nodes under the bookstore element:
          下面這個(gè)實(shí)例就會(huì)選擇所有bookstore元素以下的book節(jié)點(diǎn):

          xmlDoc.selectNodes("/bookstore/book")

          如果你有IE5以上的版本你可以自己來(lái)做一下.


          Select the First book Node
          選擇第一個(gè)book節(jié)點(diǎn)

          The following example selects only the first book node under the bookstore element:

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

          If you have IE 5 or higher you can try it yourself.

          Note: IE 5 and 6 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!


          Select the prices
          選擇prices

          The following example selects the text from all the price nodes:

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

          If you have IE 5 or higher you can try it yourself.


          Selecting price Nodes with Price>35
          選擇price大于35的price節(jié)點(diǎn)

          The following example selects all the price nodes with a price higher than 35:

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

          If you have IE 5 or higher you can try it yourself.


          Selecting title Nodes with Price>35
          選擇Price大于35的title節(jié)點(diǎn)

          The following example selects all the title nodes with a price higher than 35:

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

          If you have IE 5 or higher you can try it yourself.

          主站蜘蛛池模板: 大理市| 宜君县| 和平县| 共和县| 耒阳市| 潞城市| 比如县| 闸北区| 老河口市| 兴业县| 和林格尔县| 炎陵县| 庆元县| 三台县| 平乡县| 江都市| 黔江区| 西充县| 景谷| 绥化市| 安泽县| 缙云县| 外汇| 且末县| 万山特区| 海南省| 永春县| 长治市| 正安县| 原平市| 株洲县| 昌江| 漳平市| 温宿县| 青冈县| 江永县| 开江县| 库尔勒市| 朝阳县| 葫芦岛市| 东源县|