The currently selected answer is generally incorrect!
<xsl:if test="not(following-sibling::Unit)">
This Will not work with any XML document and any <xsl:apply-templates>
The original question is about the last Unit
being matched, not the last sibling! Which is the last Unit being matched depends only on the expression in the select attribute of <xsl:apply-templates>
, not on the physical properties of the XML document.
The way to do it:
<xsl:apply-templates select="SomeExpression"/>
then in the template that matches nodes selected by SomeExpression
:
<xsl:if test="position() = last()">
. . . .
</xsl:if>
This checks if the current node is the last in the node-list
selected by <xsl:apply-templates>
, not that the current node is the last sibling. This answers exactly the original question.
If the question was framed in a different way, asking how to recognize if the last sibling Unit
is the current node, then the best solution would be to specify a separate template for this last sibling node:
<xsl:template match="Unit[last()]">
. . . .
</xsl:template>
Do note, that in this case there is no need to write any conditional logic inside a template to test if the current node is "the last".