XSLT - 从URL获取文件名

CLi*_*own 4 xml xslt xpath xslt-1.0

我需要从URL获取文件名,URL是动态的,斜杠的数量可以是不同的.我正在使用xslt 1.0,所以寻找将需要的东西:

HTTP://DevSite/sites/name/Lists/note/Attachments/3/image.jpg

并告诉我:

image.jpg的

在XSLT 1.0中这可能吗?

Phi*_*yNJ 6

如果您使用的是xslt 2.0,则可以使用subsequence()并创建一个函数:

在xsl:stylesheet root中声明你的函数:

xmlns:myNameSpace="http://www.myNameSpace.com/myfunctions"
Run Code Online (Sandbox Code Playgroud)

创建功能:

<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>
Run Code Online (Sandbox Code Playgroud)

调用函数:

<xsl:value-of select="myNameSpace:getFilename('http://DevSite/sites/name/Lists/note/Attachments/3/image.jpg', '/')"/>
Run Code Online (Sandbox Code Playgroud)


Kir*_*huk 5

你可以使用递归:

<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>
Run Code Online (Sandbox Code Playgroud)

输出: image.jpg