Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT - Get file name from URL

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?

like image 333
CLiown Avatar asked Aug 09 '11 11:08

CLiown


2 Answers

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', '/')"/>
like image 76
PhillyNJ Avatar answered Oct 22 '22 04:10

PhillyNJ


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

like image 6
Kirill Polishchuk Avatar answered Oct 22 '22 03:10

Kirill Polishchuk