Reverse Find in a string
xml, xslt
Solution
The following is an example of a template that would produce the required output in XSLT 1.0:
<xsl:template name="getExtension">
<xsl:param name="filename"/>
<xsl:choose>
<xsl:when test="contains($filename, '.')">
<xsl:call-template name="getExtension">
<xsl:with-param name="filename" select="substring-after($filename, '.')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$filename"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="getExtension">
<xsl:with-param name="filename" select="'http://www.blah.com/path/to/file/media.jpg'"/>
</xsl:call-template>
</xsl:template>
Problem
I need to be able to find the last occurrence of a character within an element. For example: ``` <mediaurl>http://www.blah.com/path/to/file/media.jpg</mediaurl> ``` If I try to locate it through using `substring-before(mediaurl, '.')` and `substring-after(mediaurl, '.')` then it will, of course, match on the first dot. How would I get the file extension? Essentially, I need to get the file name and the extension from a path like this, but I am quite stumped as to how to do it using XSLT.