I need to obtain the filename from a URL, the URL is dynamic and the amount of slashes can be different amounts. Im using xslt 1.0 so looking for something that will take:
http://DevSite/sites/name/Lists/note/Attachments/3/image.jpg
and give me:
image.jpg
IS this possible in XSLT 1.0?
if you are using xslt 2.0, you can use subsequence() and create a function:
Declare your function in xsl:stylesheet root:
xmlns:myNameSpace="http://www.myNameSpace.com/myfunctions"
Create the function:
<xsl:function name="myNameSpace:getFilename">
<xsl:param name="str"/>
<!--str e.g. document-uri(.), filename and path-->
<xsl:param name="char"/>
<xsl:value-of select="subsequence(reverse(tokenize($str, $char)), 1, 1)"/>
</xsl:function>
Call the function:
<xsl:value-of select="myNameSpace:getFilename('http://DevSite/sites/name/Lists/note/Attachments/3/image.jpg', '/')"/>
You can use recursion:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="url">http://DevSite/sites/name/Lists/note/Attachments/3/image.jpg</xsl:variable>
<xsl:call-template name="get-file-name">
<xsl:with-param name="input" select="$url"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="get-file-name">
<xsl:param name="input"/>
<xsl:choose>
<xsl:when test="contains($input, '/')">
<xsl:call-template name="get-file-name">
<xsl:with-param name="input" select="substring-after($input, '/')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output: image.jpg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With