[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: last index of...

Subject: Re: last index of...
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 12 Nov 2001 18:20:08 +0000
substring after last
Hi Utah,

> Is there a function in XSLT which obtains the position of the last
> occurrence of a string within another string?

No, there isn't. To get the substring after the last occurrence of a
string, you need a recursive template, for example:

<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>

For example to get the extension of a file from its path you could
use:

  <xsl:call-template name="substring-after-last">
    <xsl:with-param name="string" select="$file" />
    <xsl:with-param name="delimiter" select="'.'" />
  </xsl:call-template>

Getting the last index would be slightly more complicated, and it
looks like you want to use it to get hold of the string after the last
occurrence of something, so hopefully this is sufficient.
  
[Note that the XQuery/XPath operators document contains an ends-with()
function so it's possible that in XPath 2.0 if you wanted to *test*
the value of the string after the last '.' to see if it was 'xml' then
you could do:

  ends-with($file, '.xml')

but that's just speculation at the moment.]

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.