Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old November 8th, 2011, 08:53 PM
Registered User
 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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?
 
Old November 9th, 2011, 02:27 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

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
 
Old November 9th, 2011, 02:35 AM
Registered User
 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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?
 
Old November 9th, 2011, 03:39 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Would the follow help:
Code:
<xsl:value-of select="tokenize(@char,',')[last()]"/>
__________________
Rummy
 
Old November 9th, 2011, 06:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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:
kvbhaskar7 (November 14th, 2011)
 
Old November 14th, 2011, 04:17 PM
Registered User
 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding last instance of a character tclotworthy XSLT 3 September 27th, 2007 11:22 AM
trim a string to the first character jd_erd Access 2 May 6th, 2007 11:01 PM
Delete last a character in string dtho Excel VBA 27 December 7th, 2005 01:01 PM
finding the string before a special character srini XSLT 2 December 16th, 2003 07:57 AM
Finding a specific character within a string tp194 Classic ASP Databases 2 October 12th, 2003 10:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.