Selecting a last word from a string in xslt
11,195
XSLT/Xpath 2.0 - using the tokenize() function to split the string on the "-" and then use a predicate filter to select the last item in the sequence:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="tokenize('aaa-bbb-ccc-ddd','-')[last()]"/>
</xsl:template>
</xsl:stylesheet>
XSLT/XPath 1.0 - using a recursive template to look for the last occurrence of "-" and selecting the substring following it:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="input" select="'aaa-bbb-ccc-ddd'" />
<xsl:with-param name="marker" select="'-'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="substring-after-last">
<xsl:param name="input" />
<xsl:param name="marker" />
<xsl:choose>
<xsl:when test="contains($input,$marker)">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="input"
select="substring-after($input,$marker)" />
<xsl:with-param name="marker" select="$marker" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 19, 2022Comments
-
Admin almost 2 years
I just want to take the last element from a string which is like this "aaa-bbb-ccc-ddd" in xslt.
Out put should be "ddd" irrecspective of '-'s.