 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 8th, 2011, 08:53 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Finding the last occurance of character in the given string?
Hi,
Is there any way to extract/find the last occurance of character in a string.
I have a string like "12,34,567" and i need to seperate 12,34 and assign a HTML class for last occurance of "," and append 567.
I have used the below code, but it is working only for first occurance.
<xsl:when test="../@char = ','">
<xsl:value-of select="substring-before(.,',')"/>
<span class="Align">,
</span>
<xsl:value-of select="substring-after(.,',')"/>
</xsl:when>
Any other suggestions?
|
|

November 9th, 2011, 02:27 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
If you are sure that there will be only three values, then you can use the following:
Code:
<xsl:value-of select="substring-after(substring-after(@char,','),',')"/>
which will give the value 567.
__________________
Rummy
|
|

November 9th, 2011, 02:35 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thanks for your reply.
No, the value will change and number of "," occurances also changes. Is there any way to find out the last occurance?
|
|

November 9th, 2011, 03:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
Would the follow help:
Code:
<xsl:value-of select="tokenize(@char,',')[last()]"/>
__________________
Rummy
|
|

November 9th, 2011, 06:28 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
In XSLT 2.0, use the tokenize() function. In XSLT 1.0, you need to write a recursive template. Or rather you don't, because it's already been written - you can download the EXSLT str:tokenize template from www.exslt.org
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
The Following User Says Thank You to mhkay For This Useful Post:
|
|
|

November 14th, 2011, 04:17 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Found this recursive template which helped me to resolve the issue
Code:
<xsl:template name="substring-after-last">
<xsl:param name="string" />
<xsl:param name="delimiter" />
<xsl:choose>
<xsl:when test="contains($string, $delimiter)">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string"
select="substring-after($string, $delimiter)" />
<xsl:with-param name="delimiter" select="$delimiter" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
|
|
 |