跳轉到內容

XML - 資料交換管理/XPath

來自華夏公益教科書,開放的世界開放的書籍



上一章 下一章
XHTML XLink



學習目標

  • 能夠將 XML 文件概念化為節點樹
  • 引用 XML 文件中的元素組
  • 理解簡寫和非簡寫 XPath 語法之間的區別
  • 理解絕對路徑和相對路徑之間的區別
  • 能夠使用 XPath 謂詞和函式來細化 XPath 的節點集

在前面的章節中,您已經學習了 XSL 的基本概念,以及在執行 XSL 轉換時必須如何引用 XML 文件中的節點。到目前為止,您一直在使用一種簡單的語法來引用 XML 文件中的節點。雖然您迄今為止使用的語法一直是 XPath,但您將在本章中學習更多功能和功能。當您開始理解路徑語言如何用於引用 XML 文件中的節點時,您對 XML 作為樹形結構的理解將開始就位。本章包含演示 XPath 許多常見用法的示例,但有關完整的 XPath 規範,請參閱標準的最新版本:

http://www.w3.org/TR/xpath

XSL 大量使用 XPath。

當您在命令提示符下複製檔案或“cd”到目錄時,您通常會鍵入類似“/home/darnell/”的內容來引用資料夾。這使您能夠在整個計算機的檔案系統中更改或引用資料夾。XML 具有類似的方式來引用 XML 文件中的元素。這種特殊的語法稱為 XPath,它是 XML 路徑語言的縮寫。

XPath 是一種用於在 XML 文件中查詢資訊的語言。XPath 用於在 XML 文件中遍歷元素和屬性。

XPath 雖然用於引用 XML 樹中的節點,但它本身不是用 XML 編寫的。這是 W3C 明智的選擇,因為試圖在 XML 中指定路徑資訊將是一項非常繁瑣的任務。任何構成 XML 語法的字元都需要轉義,以便在處理時不會與 XML 混淆。XPath 也很簡潔,允許您以非常高的特異性呼叫 XML 樹中的節點,而不會顯得過於冗長。

XML 作為樹形結構

[編輯 | 編輯原始碼]

XML 的一大好處是文件本身描述了資料的結構。如果您中的任何人都研究過您的家族史,您可能已經遇到過家譜。樹的頂部是某個早期的祖先,而樹的底部是最近的孩子。

透過樹形結構,您可以看到哪些孩子屬於哪些父母,哪些孫子屬於哪些祖父母等等。

XML 的妙處在於它也很適合這種樹形結構,通常被稱為 XML 樹。

理解節點關係

[編輯 | 編輯原始碼]

我們將使用以下示例來演示不同的節點關係。

<bookstore>
	<book>
		<title>Less Than Zero</title>
		<author>Bret Easton Ellis</author>
		<year>1985</year>
		<price>13.95</price>
	</book>
</bookstore>
父節點
每個元素和屬性都有一個父節點。
book 元素是 title、author、year 和 price 的父節點
子節點
元素節點可以有零個、一個或多個子節點。
title、author、year 和 price 元素都是 book 元素的子節點
兄弟姐妹
具有相同父節點的節點。
title、author、year 和 price 元素都是兄弟姐妹
祖先
節點的父節點、父節點的父節點等等。
title 元素的祖先是 book 元素和 bookstore 元素
後代
節點的子節點、子節點的子節點等等。
bookstore 元素的後代是 book、title、author、year 和 price 元素

此外,將 XML 檔案同時視為序列化檔案(例如,您在 XML 編輯器中看到的)在某些方面仍然有用。這樣您就可以理解前一個節點和後一個節點的概念。如果原始節點在文件順序中位於另一個節點之前,則該節點被認為在另一個節點之前。同樣,如果一個節點在另一個節點之後出現在文件順序中,則它在該節點之後。祖先和後代不被認為是前一個或後一個節點。這個概念在稍後討論軸的概念時會派上用場。

簡寫與非簡寫 XPath 語法

[編輯 | 編輯原始碼]

XPath 的建立是為了簡潔地引用節點,同時保留搜尋多種選項的能力。XPath 的大多數用法將涉及搜尋特定節點的子節點、父節點或屬性節點。由於這些用法非常普遍,因此可以使用簡寫語法來引用這些常用的節點。以下是模擬樹(有葉子和樹枝的樹)的 XML 文件。它將用於演示不同型別的語法。

<?xml version="1.0" encoding="UTF-8"?>
    <trunk name="the_trunk">
        <bigBranch name="bb1" thickness="thick">
            <smallBranch name="sb1">
                <leaf name="leaf1" color="brown" />
		<leaf name="leaf2" weight="50" />
		<leaf name="leaf3" />
	    </smallBranch>
	    <smallBranch name="sb2">
                <leaf name="leaf4" weight="90" />
		<leaf name="leaf5" color="purple" />
            </smallBranch>
        </bigBranch>
        <bigBranch name="bb2">
            <smallBranch name="sb3">
		<leaf name="leaf6" />
	    </smallBranch>
	    <smallBranch name="sb4">
		<leaf name="leaf7" />
		<leaf name="leaf8" />
		<leaf name="leaf9" color="black" />
		<leaf name="leaf10" weight="100" />
            </smallBranch>
        </bigBranch>
    </trunk>

示例 9.2:tree. xml – 示例 XML 頁面

以下是一些 XPath 位置路徑的示例,分別用英語、簡寫 XPath 和非簡寫 XPath 表示。

選擇 1

英語:此文件中所有是 trunk 的子節點的 bigBranch 的子節點的 smallBranch 的子節點的 leaf 元素,trunk 是 root 的子節點。
簡寫:/trunk/bigBranch/smallBranch/leaf
非簡寫:/child::trunk/child::bigBranch/child::smallBranch/child::leaf

選擇 2

英語:具有“name”屬性等於“bb3”的 bigBranch 元素,它們是 trunk 元素的子節點,trunk 元素是 root 的子節點。
簡寫:/trunk/bigBranch[@name=’bb3’]
非簡寫:/child::trunk/child::bigBranch[attribute::name=’bb3’]

請注意,在前面的示例中,我們如何使用謂詞來指定要哪些 bigBranch 物件。這將搜尋範圍縮小到僅滿足謂詞的 bigBranch 節點。謂詞是 XPath 語句中位於方括號內的部分。在本例中,謂詞要求“name”屬性設定為“bb3”的 bigBranch 節點。

最後兩個示例假設我們想要指定從根節點開始的路徑。現在讓我們假設我們正在指定從 <smallBranch> 節點開始的路徑。

選擇 3

英語:當前 <smallBranch> 的父節點。(請注意,此選擇相對於 <smallBranch>)
簡寫 ..
非簡寫:parent::node()

當使用非簡寫語法時,您可能會注意到您正在呼叫一個父節點或子節點,後面跟著兩個冒號(::)。每個冒號都稱為一個。您將在稍後瞭解有關軸的更多資訊。

此外,現在可能是解釋位置路徑概念的好時機。位置路徑是到達所選節點的一系列位置步驟。位置步驟是 XPath 語句中以“/”字元分隔的部分。它們是找到要選擇的節點的路徑上的一個步驟。

位置步驟由三個部分組成:軸(子節點、父節點、後代節點等)、節點測試(節點名稱或檢索一個或多個節點的函式)以及一系列謂詞(對檢索到的節點的測試,以縮小結果範圍,排除不透過謂詞測試的節點)。

因此,在位置路徑中,它的每個位置步驟都會返回一個節點列表。如果在某個位置步驟之後,路徑上還有其他步驟,則下一個步驟將在該步驟返回的所有節點上執行。

相對路徑與絕對路徑

[edit | edit source]

在使用 XPath 指定路徑時,有時您已經在某個節點“內”。但其他時候,您可能希望從根節點開始選擇節點。XPath 允許您同時執行這兩種操作。如果您曾經使用過 HTML 中的網站,它的工作原理與在 HTML 超連結中引用其他檔案相同。在 HTML 中,您可以為超連結指定絕對路徑,在 URL 中描述另一個頁面位於伺服器名稱、資料夾和檔名中的位置。或者,如果您引用同一站點上的另一個檔案,則無需輸入伺服器名稱或所有路徑資訊。這稱為相對路徑。該概念可以在 XPath 中以類似的方式應用。

您可以透過 XPath 表示式開頭是否有“/”字元來判斷兩者之間的區別。如果有,則路徑是從根節點指定的,這使其成為絕對路徑。但如果路徑開頭沒有“/”,則您正在指定一個相對路徑,該路徑描述了其他節點相對於上下文節點(或正在執行下一步的節點)的位置。

以下是用於我們上面樹狀 .xml 檔案(示例 9.2)的 XSL 樣式表(示例 9.3)。

<?xml version="1.0" encoding="UTF-8" ?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>

<!-- Example of an absolute link. The element '/child::trunk'
 is being specified from the root element. -->

 <xsl:template match="/child::trunk">

<html>
    <head>
        <title>XPath Tree Tests</title>
    </head>
     <body>

<!-- Example of a relative link. The <for-each> xsl statement will
    execute for every <bigBranch> node in the
    ‘current’ node, which is the <trunk>node. -->

 <xsl:for-each select="child::bigBranch">

         <xsl:call-template name="print_out" />
           </xsl:for-each>
        </body>
   </html>
</xsl:template>
      <xsl:template name="print_out">
             <xsl:value-of select="attribute::name" /> <br />
   </xsl:template>
 </xsl:stylesheet>

示例 9.3:xsl_tree.xsl – 相對路徑和絕對路徑的示例

四種 XPath 位置路徑

[edit | edit source]

在最後兩節中,您瞭解了區分不同位置路徑的兩種不同區別:非縮寫與縮寫,相對路徑與絕對路徑。結合這兩個概念可能有助於談論 XPath 位置路徑。更不用說,當您說以下內容時,它可以讓您在朋友面前顯得非常聰明

  1. 縮寫相對位置路徑 - 在指定相對路徑時使用縮寫語法。
  2. 縮寫絕對位置路徑 - 在指定絕對路徑時使用縮寫語法。
  3. 非縮寫相對位置路徑 - 在指定相對路徑時使用非縮寫語法。
  4. 非縮寫絕對位置路徑 - 在指定絕對路徑時使用非縮寫語法。

我只在現在提到這種四路區分,因為它在閱讀規範或其他關於該主題的文字時可能會有所幫助。

XPath 軸

[edit | edit source]

在 XPath 中,某些節點選擇需要非縮寫語法才能提高效能。在這種情況下,您將使用軸來指定在位置路徑中透過的每個位置步驟。

從樹中的任何節點,您都可以沿 13 個軸進行操作。它們如下所示

含義
ancestor: 從當前節點到根節點的父節點
ancestor-or-self: 從當前節點到根節點的父節點以及當前節點
attribute: 當前節點的屬性
child: 當前節點的直接子節點
descendant: 當前節點的子節點(包括子節點的子節點)
descendant-or-self: 當前節點的子節點(包括子節點的子節點)以及當前節點
following: 當前節點之後的節點(不包括子節點)
following-sibling: 當前節點之後的節點(不包括子節點)處於同一級別
namespace: XML namespace of the current node
parent: 當前節點的直接父節點
preceding: 當前節點之前的節點(不包括子節點)
preceding-sibling: 當前節點之前的節點(不包括子節點)處於同一級別
self: 當前節點

XPath 謂詞和函式

[edit | edit source]

有時,您可能希望在 XPath 位置路徑中使用謂詞來進一步過濾您的選擇。通常,您會從位置路徑中獲得一組節點。謂詞是一個小的表示式,它會針對一組節點中的每個節點進行評估。如果表示式評估為“false”,則該節點不會包含在選擇中。以下是一個示例

//p[@class=‘alert’]

在前面的示例中,文件中的每個 <p> 標籤都將被檢查,以檢視其“class”屬性是否設定為“alert”。只有那些“class”屬性值為“alert”的 <p> 標籤才會包含在此位置路徑的節點集中。

以下示例使用一個函式,該函式可以在謂詞中使用,以獲取有關上下文節點的資訊。

/book/chapter[position()=3]

前面的示例僅選擇第 3 位的書籍章節。因此,為了返回某些內容,當前 <book> 元素必須至少有 3 個 <chapter> 元素。

還要注意,position 函式返回一個整數。XPath 規範中有許多函式。有關完整列表,請參閱 W3C 規範 http://www.w3.org/TR/xpath#corelib

以下是一些可能會有所幫助的其他函式

number last() – 當前節點集中的最後一個節點

number position() – 正在測試的上下文節點的位置

number count(node-set) – 節點集中節點的數量

boolean starts-with(string, string) – 如果第一個引數以第二個引數開頭,則返回 true

boolean contains(string, string) – 如果第一個引數包含第二個引數,則返回 true

number sum(node-set) – 節點集中節點的數值之和

number floor(number) – 數值,向下舍入到最接近的整數

number ceiling(number) – 數值,向上舍入到最接近的整數

number round(number) – 數值,舍入到最接近的整數

示例

[edit | edit source]

以下 XML 文件、XSD 架構和 XSL 樣式表示例旨在幫助您將本章中學習的所有內容結合使用真實資料。在學習此示例時,您會注意到如何使用 XPath 在樣式表中呼叫和修改從文件中提取的特定資訊的輸出。

以下是一個 XML 文件(示例 9.4)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="movies.xsl" type="text/xsl" media="screen"?>
<movieCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="movies.xsd">

<movie>
    <movieTitle>Meet the Parents</movieTitle>
    <movieSynopsis>
    Greg Focker is head over heels in love with his girlfriend Pam, and is ready to
    pop the big question. When his attempt to propose is thwarted by a phone call
    with the news that Pam's younger sister is getting married, Greg realizes that
    the key to Pam's hand in marriage lies with her formidable father.
    </movieSynopsis>
    <role>
        <roleIDREF>bs1</roleIDREF>
        <roleType>Lead Actor</roleType>
    </role>
    <role>
        <roleIDREF>tp1</roleIDREF>
        <roleType>Lead Actress</roleType>
    </role>
    <role>
        <roleIDREF>rd1</roleIDREF>
        <roleType>Lead Actor</roleType>
    </role>
    <role>
        <roleIDREF>bd1</roleIDREF>
        <roleType>Supporting Actress</roleType>
    </role>
</movie>

<movie>
    <movieTitle>Elf</movieTitle>
    <movieSynopsis>
    One Christmas Eve, a long time ago, a small baby at an orphanage crawled into
    Santa’s bag of toys, only to go undetected and accidentally carried back to Santa’s
    workshop in the North Pole. Though he was quickly taken under the wing of a surrogate
    father and raised to be an elf, as he grows to be three sizes larger than everyone else,
    it becomes clear that Buddy will never truly fit into the elf world. What he needs is
    to find his real family. This holiday season, Buddy decides to find his true place in the
    world and sets off for New York City to track down his roots.
    </movieSynopsis>
    <role>
        <roleIDREF>wf1</roleIDREF>
        <roleType>Lead Actor</roleType>
    </role>
    <role>
        <roleIDREF>jc1</roleIDREF>
        <roleType>Supporting Actor</roleType>
    </role>
    <role>
        <roleIDREF>zd1</roleIDREF>
        <roleType>Lead Actress</roleType>
    </role>
    <role>
        <roleIDREF>ms1</roleIDREF>
        <roleType>Supporting Actress</roleType>
    </role>
    </movie>

<castMember>
    <castMemberID>rd1</castMemberID>
    <castFirstName>Robert</castFirstName>
    <castLastName>De Niro</castLastName>
    <castSSN>489-32-5984</castSSN>
    <castGender>male</castGender>
</castMember>

<castMember>
    <castMemberID>bs1</castMemberID>
    <castFirstName>Ben</castFirstName>
    <castLastName>Stiller</castLastName>
    <castSSN>590-59-2774</castSSN>
    <castGender>male</castGender>
</castMember>

<castMember>
    <castMemberID>tp1</castMemberID>
    <castFirstName>Teri</castFirstName>
    <castLastName>Polo</castLastName>
    <castSSN>099-37-8765</castSSN>
    <castGender>female</castGender>
</castMember>

<castMember>
    <castMemberID>bd1</castMemberID>
    <castFirstName>Blythe</castFirstName>
    <castLastName>Danner</castLastName>
    <castSSN>273-44-8690</castSSN>
    <castGender>male</castGender>
</castMember>

<castMember>
    <castMemberID>wf1</castMemberID>
    <castFirstName>Will</castFirstName>
    <castLastName>Ferrell</castLastName>
    <castSSN>383-56-2095</castSSN>
    <castGender>male</castGender>
</castMember>

<castMember>
    <castMemberID>jc1</castMemberID>
    <castFirstName>James</castFirstName>
    <castLastName>Caan</castLastName>
    <castSSN>389-49-3029</castSSN>
    <castGender>male</castGender>
</castMember>

<castMember>
    <castMemberID>zd1</castMemberID>
    <castFirstName>Zooey</castFirstName>
    <castLastName>Deschanel</castLastName>
    <castSSN>309-49-4005</castSSN>
    <castGender>female</castGender>
</castMember>

<castMember>
    <castMemberID>ms1</castMemberID>
    <castFirstName>Mary</castFirstName>
    <castLastName>Steenburgen</castLastName>
    <castSSN>988-43-4950</castSSN>
    <castGender>female</castGender>
</castMember>

</movieCollection>

示例 9.4:movies_xpath.xml

以下是一個 XML 文件(示例 9.5)

<?xml version="1.0" encoding="UTF-8"?>

<cities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="cities.xsd">

<city>
    <cityID>c2</cityID>
    <cityName>Mandal</cityName>
    <cityPopulation>13840</cityPopulation>
    <cityCountry>Norway</cityCountry>
    <tourismDescription>A small town with a big atmosphere.  Mandal provides comfort
away from normal luxuries.
    </tourismDescription>
    <capitalCity>c3</capitalCity>
</city>

<city>
    <cityID>c3</cityID>
    <cityName>Oslo</cityName>
    <cityPopulation>533050</cityPopulation>
    <cityCountry>Norway</cityCountry>
    <tourismDescription>Oslo is the capital of Norway for many reasons.
    It is also the capital location for tourism.  The culture, shopping,
    and attractions can all be experienced in Oslo.  Just remember
    to bring your wallet.
    </tourismDescription>
</city>

</cities>

示例 9.5:cites__xpath.xml

以下是“電影”架構(示例 9.6)

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">

  <!--Movie Collection-->

  <xsd:element name="movieCollection">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="movie" type="movieDetails" minOccurs="1" maxOccurs="unbounded"/>

      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <!--This contains the movie details.-->

  <xsd:complexType name="movieDetails">
    <xsd:sequence>
      <xsd:element name="movieTitle" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
      <xsd:element name="movieSynopsis" type="xsd:string"/>
      <xsd:element name="role" type="roleDetails" minOccurs="1" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

 <!--The contains the genre details.-->

  <xsd:complexType name="roleDetails">
    <xsd:sequence>
       <xsd:element name="roleIDREF" type="xsd:IDREF"/>
       <xsd:element name="roleType" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:simpleType name="ssnType">
       <xsd:restriction base="xsd:string">
           <xsd:pattern value="\d{3}-\d{2}-\d{4}"/>
       </xsd:restriction>
   </xsd:simpleType>

 <xsd:complexType name="castDetails">
    <xsd:sequence>
       <xsd:element name="castMemberID" type="xsd:ID"/>
       <xsd:element name="castFirstName" type="xsd:string"/>
       <xsd:element name="castLastName" type="xsd:string"/>
       <xsd:element name="castSSN" type="ssnType"/>
       <xsd:element name="castGender" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>

示例 9.6:movies.xsd

以下是“城市”架構(示例 9.7)

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xsd:element name="cities">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="city" type="cityType" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:complexType name="cityType">
  <xsd:sequence>
    <xsd:element name="cityID" type="xsd:ID"/>
     <xsd:element name="cityName" type="xsd:string"/>
     <xsd:element name="cityPopulation" type="xsd:integer"/>
     <xsd:element name="cityCountry" type="xsd:string"/>
     <xsd:element name="tourismDescription" type="xsd:string"/>
     <xsd:element name="capitalCity" type="xsd:IDREF" minOccurs="0" maxOccurs="1"/>
  </xsd:sequence>
</xsd:complexType>
</xsd:schema>

示例 9.7:cities.xsd

以下是 XSL 樣式表(示例 9.8)

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="castList" match="castMember" use="castMemberID"/>
<xsl:output method="html"/>

<!-- example of using an abbreviated absolute path to pull info
from cities_xpath.xml for the city "Oslo" specifically -->

<!-- specify absolute path to select cityName and assign it the variable "city" -->
<xsl:variable name="city" select="document('cities_xpath.xml')
/cities/city[cityName='Oslo']/cityName" />

<!-- specify absolute path to select cityCountry and assign it the variable "country" -->
<xsl:variable name="country" select="document('cities_xpath.xml')
/cities/city[cityName='Oslo']/cityCountry" />

<!-- specify absolute path to select tourismDescription and assign it the variable "description" -->
<xsl:variable name="description" select="document('cities_xpath.xml')
/cities/city[cityName='Oslo']/tourismDescription" />

<xsl:template match="/">
<html>
    <head>
        <title>Movie Collection</title>
    </head>
    <body>
        <h2>Movie Collection</h2>
    <xsl:apply-templates select="movieCollection"/>
    </body>
</html>
</xsl:template>
<xsl:template match="movieCollection">

<!-- let's say we just want to see the actors. -->
<!--
<xsl:for-each select="movie">
<hr />
<br />
<b><xsl:text>Movie Title: </xsl:text></b>
<xsl:value-of select="movieTitle"/>
<br />
<br />
<b><xsl:text>Movie Synopsis: </xsl:text></b>
<xsl:value-of select="movieSynopsis"/>
<br />
<br />-->

<!-- actor info begins here. -->
<b><xsl:text>Cast: </xsl:text></b>
<br />
<!-- specify an abbreviated relative path here for "role."
NOTE: there is no predicate in this one; it's just a path. -->

<xsl:for-each select="movie/role">
<xsl:sort select="key('castList',roleIDREF)/castLastName"/>
<xsl:number value="position()" format="&#xa; 0. " />
<xsl:value-of select="key('castList',roleIDREF)/castFirstName"/>
<xsl:text>   </xsl:text>
<xsl:value-of select="key('castList',roleIDREF)/castLastName"/>
<xsl:text>,   </xsl:text>
<xsl:value-of select="roleType"/>
<br />
<xsl:value-of select="key('castList',roleIDREF)/castGender"/>
<xsl:text>,   </xsl:text>
<xsl:value-of select="key('castList',roleIDREF)/castSSN"/>
<br />
<br />
</xsl:for-each>
<!--
</xsl:for-each>-->
<hr />

<!--calling the variables -->

<span style="color:red;">
<p><b>Travel Advertisement</b></p>

<!-- reference the city, followed by a comma, and then the country -->
<p><xsl:value-of select="$city" />, <xsl:value-of select="$country" /></p>

<!-- reference the description -->
<xsl:value-of select="$description" />

</span>
</xsl:template>
</xsl:stylesheet>

示例 9.6:movies.xsl

摘要

[edit | edit source]
在本章中,我們學習了 XML 路徑語言的許多功能和能力。您現在應該對透過使用 XML 樹結構的節點關係有了很好的理解。使用縮寫和非縮寫位置路徑的概念,我們可以透過滿足方括號中的謂詞來縮小搜尋範圍,僅找到特定元素。相對路徑和絕對路徑用於指定路徑到您的位置。相對路徑給出了檔案相對於當前工作目錄的位置,而絕對路徑給出了計算機或檔案系統中檔案或目錄名稱的精確位置。這兩個概念可以組合起來得出四種類型的 XPath 位置路徑:縮寫相對路徑、縮寫絕對路徑、非縮寫相對路徑,以及最後非縮寫絕對路徑。如果需要進一步過濾,可以使用 XPath 謂詞和函式。這些允許對謂詞進行評估,例如 true/false 和計數函式。如果正確使用,XPath 可以成為 XML 語言中非常強大的工具。
華夏公益教科書