This is essentially the same solution, but to save intermediate delimiters as a result, you must add the following three lines :
<xsl:if test="contains(substring-after($pText, $pDelim), $pDelim)"> <xsl:value-of select="$pDelim"/> </xsl:if>
Now the whole transformation becomes the following:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="s" select="'ABC_12345_Q-10'"/> <xsl:template match="/"> <xsl:call-template name="stripLast"> <xsl:with-param name="pText" select="$s"/> </xsl:call-template> </xsl:template> <xsl:template name="stripLast"> <xsl:param name="pText"/> <xsl:param name="pDelim" select="'_'"/> <xsl:if test="contains($pText, $pDelim)"> <xsl:value-of select="substring-before($pText, $pDelim)"/> <xsl:if test="contains(substring-after($pText, $pDelim), $pDelim)"> <xsl:value-of select="$pDelim"/> </xsl:if> <xsl:call-template name="stripLast"> <xsl:with-param name="pText" select= "substring-after($pText, $pDelim)"/> <xsl:with-param name="pDelim" select="$pDelim"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
When this conversion is applied to any XML document (not used), the desired, correct result is obtained :
ABC_12345
source share