mirror of
https://github.com/alexandrev/xslt-lab.git
synced 2026-09-18 19:43:16 +00:00
b9d58ff7e0
- 19 reference pages under /xslt/functions/ covering: xsl:value-of, xsl:for-each, xsl:for-each-group, xsl:function, xsl:iterate, last(), position(), count(), concat(), contains(), substring(), sum(), key(), current(), string-join(), tokenize(), matches(), replace(), format-date() - Each page: description, parameters table, return value, 2 runnable examples (input XML + stylesheet + output), notes, cross-links - New Hugo section with reference-specific layout (version badges, syntax block, "Try in Playground" CTA, parameters table styles) - Menu entry "Reference" → /xslt/functions/ - CSS: .ref-badge, .ref-syntax, .ref-index-list, .ref-try + table styles Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2.6 KiB
2.6 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| last() | Returns the size of the context node-set, i.e. the index of the last item in the current iteration context. | 2026-04-18T00:00:00Z | 1.0 | XSLT 1.0 | node function | last() |
|
Description
last() returns an integer equal to the context size — the total number of nodes in the node-set (or items in the sequence) currently being processed. It takes no arguments.
The most common use cases are:
- Detecting the last item in a loop to apply different formatting (e.g., omitting a trailing separator).
- Building predicates like
item[last()]to select only the final element in a set. - Combining with
position()to produce row counts or progress labels.
The value of last() changes with the context. Inside xsl:for-each, it reflects the number of items selected by the select attribute. Inside a template triggered by xsl:apply-templates, it reflects the number of nodes sent to that template call.
Return value
xs:integer — the total number of items in the current context sequence.
Examples
Omit trailing comma
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<colors>
<color>Red</color>
<color>Green</color>
<color>Blue</color>
</colors>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/colors">
<xsl:for-each select="color">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Red, Green, Blue
Select only the last element
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/colors">
<xsl:value-of select="color[last()]"/>
</xsl:template>
</xsl:stylesheet>
Output:
Blue
Notes
last()andposition()are context-dependent. Their values inside a predicate differ from their values in the body ofxsl:for-each— predicates establish their own context.- In XSLT 2.0+ with typed sequences,
last()still works the same way but the context is anxs:integer-typed sequence size. - A common mistake is calling
last()outside any iterating context; at the top level of a template, it returns 1 (the single context node).