共用方式為


last 函式 (XPath)

傳回等於運算式評估內容大小的數目。

number last()

備註

下列將尋找每一個 <book> 項目的最後一個 <author> 子系:

book/author[last()]

範例

在這個範例中,我們將說明如何使用 last() 函式,選取下列 XML 文件的每個 x 項目所包含的最後一個 y 項目。

XML 檔 (test.xml)

<?xml version="1.0"?>
<!DOCTYPE test [
  <!ELEMENT test (x+)>
  <!ELEMENT x (x+| y+)>
  <!ATTLIST x
     a ID #REQUIRED>
  <!ELEMENT y ANY>
]>
<test>
    <x a="a11">
      <x a="a21">
        <x a="a31">
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>
    <x a="a12">
      <x a="a22">
        <y>y21</y>
        <y>y22</y>
      </x>
    </x>
    <x a="a13">
      <y>y11</y>
      <y>y12</y>
    </x>
    <x a="a14">
      <y>y03</y>
      <y>y04</y>
    </x>
</test>

XSLT 檔 (test.xsl)

注意這個 XSLT 樣式表中的粗體指令。

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" omit-xml-declaration="yes"/>

  <!-- Suppress text nodes not covered in subsequent  
      template rule -->
  <xsl:template match="text()"/>

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="*|@*"/>
      <xsl:if test="text()">
         <xsl:value-of select="."/>
      </xsl:if>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/test">
    <xsl:apply-templates select="//x/y[last()]"/>
  </xsl:template>

</xsl:stylesheet>

輸出

XSLT 樣式表,套用至上述 XML 檔案時,會產生下列節點集:

<y>y32</y>

<y>y22</y>

<y>y12</y>

<y>y04</y>

請參閱

參考

XML 資料型別參考