last Function
Returns a number equal to context size of the expression evaluation context.
number last()
Remarks
The following finds the last <author>
child of each <book>
element:
book/author[last()]
Example
In this example we illustrate how to use the last()
function to select the last y
elment contained in each x
element of the following XML document.
XML FILE (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="13"> <y>y11</y> <y>y12</y> </x> <x a="a14"> <y>y03</y> <y>y04</y> </x> </test>
XSLT File (test.xsl)
Pay attention to the instruction in bold in this XSLT stylesheet.
<?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>
Output
The XSLT stylesheet, when applied to the XML file above results in the following node-set:
<y>y32</y> <y>y22</y> <y>y12</y> <y>y04</y>
See Also
Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference