XSLT and XPath function reference in alphabetical order
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)
Context information within a sequence
A xs:integer integer; the position of the last item in a sequence. Simply said, fn:last() returns the number of the items of a sequence (in XPath 1.0 the number of nodes of a node set).
Purpose of use:
In XPath 2.0, fn:last() is generally applied to sequences. In XPath 1.0, the field of application of fn:last() and its sister function fn:position() only concerns node sets as they are built by xsl:apply-templates or xsl:for-each.
The function returns a xs:integer integer which corrresponds to the context size of the currently processed sequence. This value may be equal to 0. So, the function always returns exactly one integer value greater than or equal to 0. However, if the context item and with it the context size is undefined, a runtime error (err:XPDY0002) is reported.
Example – numbering all images of a document:
In a document all <image> elements shall be numbered:
<!-- compilation of the node sequence: --> <xsl:apply-templates select="//image"> <xsl:template match="image"> <p>Image <xsl:value-of select="fn:position()"/> <xsl:text> </xsl:text> of <xsl:value-of select="fn:last()"/></p> </xsl:template>
This results, for example, in the following output:
<p>Image 3 of 8</p>
In this example, the context size (number of elements) of the node sequence is determined with fn:last(); The fn:position() function determines the position of the currently processed element. The xsl:text instruction is used in this example to enforce a space character in the output.
fn:last() as xs:integer
|<< back||next >>|
Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany