Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the last entry in a comma separated string

Status
Not open for further replies.

usti

Programmer
Jan 27, 2009
2
SE
Hi

Im very new to this and Im a bit stuck on what to do.

I have a string that contains numerical values separated with commas, (3,4,2,3) and i need to get hold of the last value. My xslstylesheet is written in version 1.0 which means i cant use a tokenizer, (assuming i got that right).

Thanks.
 
[0] You can make some variant on some splitting algorithm in xslt v1.0 - a showpiece subject on the recursion use of named template.

[1] Off-hand, I can show you a sketch like this.
[tt]
<xsl:template name="output">
<xsl:param name="s" />
<xsl:value-of select="$s" />
</xsl:template>
<xsl:template name="split_string_return_last">
<xsl:param name="sin" />
<xsl:param name="separator" select="','" />
<xsl:param name="sout" />
<xsl:variable name="candidate" select="substring-after($sin,$separator)" />
<xsl:choose>
<xsl:when test="$candidate!=''">
<xsl:call-template name="split_string_return_last">
<xsl:with-param name="sin" select="$candidate" />
<xsl:with-param name="separator" select="$separator" />
<xsl:with-param name="sout" select="''" />
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:call-template name="output">
<xsl:with-param name="s" select="$sin"/>
</xsl:call-template>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
[/tt]
[2] To illustrate the use of it: suppose you at some moment in time you call that named template furnishing the comma-separated string (a parameter or a variable or a literal), it will output the last entry.
[tt]
<xsl:variable name="given" select="'3,2,4,3'" />
<xsl:call-template name="split_string_return_last">
<xsl:with-param name="sin" select="$given" />
<xsl:with-param name="separator" select="','" />
</xsl:call-template>
[/tt]
 
Thank you! I could have never figured that out, but with your help i managed to fix it, plus writing the code to get the average number.

Im very grateful. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top