跳轉到內容

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

英文:此文件中所有作為 <smallBranch> 元素的子節點、作為 <bigBranch> 元素的子節點、作為 trunk 的子節點(trunk 是 root 的子節點)的 <leaf> 元素。
簡寫:/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()

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

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

定位步驟由三個部分組成:軸(child、parents、descendant 等)、節點測試(節點的名稱,或檢索一個或多個節點的函式)以及一系列謂詞(對檢索到的節點進行的測試,縮小結果範圍,排除未透過謂詞測試的節點)。

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

相對路徑與絕對路徑

[編輯 | 編輯原始碼]

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

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

以下是一個 XSL 樣式表(展品 9.3),用於與我們上面提到的 tree.xml 檔案(展品 9.2)一起使用。

<?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 位置路徑

[編輯 | 編輯原始碼]

在最後兩個部分,您瞭解了兩種不同的區分方式來區分不同的位置路徑:簡略路徑與完整路徑,以及相對路徑與絕對路徑。將這兩個概念結合起來可以幫助您在討論 XPath 位置路徑時更加清晰。更不用說,當您在朋友面前說“簡略相對位置路徑”、“簡略絕對位置路徑”、“完整相對位置路徑”和“完整絕對位置路徑”時,會顯得非常聰明。

  1. 簡略相對位置路徑 - 在指定相對路徑時使用簡略語法。
  2. 簡略絕對位置路徑 - 在指定絕對路徑時使用簡略語法。
  3. 完整相對位置路徑 - 在指定相對路徑時使用完整語法。
  4. 完整絕對位置路徑 - 在指定絕對路徑時使用完整語法。

我現在只提到了這四種區分方式,因為在閱讀規範或其他相關文字時,它們可能會派上用場。

XPath 軸

[編輯 | 編輯原始碼]

在 XPath 中,有一些節點選擇需要使用完整語法才能提高效能。在這種情況下,您將使用一個軸來指定在位置路徑中遍歷時的每個位置步驟。

從樹中的任何節點開始,您都可以沿著 13 個軸進行遍歷。它們如下所示:

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

XPath 謂詞和函式

[編輯 | 編輯原始碼]

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

//p[@class=‘alert’]

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

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

/book/chapter[position()=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) - 將數字舍入到最接近的整數

以下 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

以下 Movies 架構(展品 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

以下 Cities 架構(展品 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

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