Trim FileName from Url, revert string

11/10/2013 10:11

<?xml version="1.0" encoding="iso-8859-1"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">

            <html>

                <body>

                    <h2>Get url part without file/item Name</h2>

                    <table border="1">

                        <tr bgcolor="#9acd32">

                            <th>FileRef</th>

                            <th>ID</th>

                            <th>List Item Link</th>

                        </tr>

                        <xsl:for-each select="dsQueryResponse/Row">

                            <tr>

                                <td>

                                    <xsl:value-of select="@FileRef"/>

                                </td>

                                <td>

                                    <xsl:value-of select="@ID"/>

                                </td>

                                <td>

                                    <xsl:value-of select="concat(@FileRef, 'DispForm.aspx?ID=', @ID)"/>

                                </td>

                                <td>

                                    <xsl:variable name="trimmedUrl">

                                        <xsl:call-template name="trimFileNameFromUrl">

                                            <xsl:with-param name="Url" select="@FileRef"/>

                                        </xsl:call-template>

                                    </xsl:variable>

                                    <xsl:value-of select="concat($trimmedUrl, '/DispForm.aspx?ID=', @ID)"/>

                                 </td>

                            </tr>

                        </xsl:for-each>

                    </table>

                </body>

            </html>

        </xsl:template>

 

<xsl:template name="trimFileNameFromUrl">

    <xsl:param name="Url"/>

    <!--Reverse string first-->

    <xsl:variable name="reversedUrl">

        <xsl:call-template name="reverse">

            <xsl:with-param name="input" select="$Url"/>

        </xsl:call-template>

    </xsl:variable>

    <!--Get substring after /-->

    <xsl:variable name="trimmedUrl">

        <xsl:value-of select='substring-after($reversedUrl, "/")'/>

    </xsl:variable>

    <!--Reverse string back-->

    <xsl:variable name='result'>

        <xsl:call-template name="reverse">

            <xsl:with-param name="input" select="$trimmedUrl"/>

        </xsl:call-template>

    </xsl:variable>

    <xsl:value-of select="$result"/>

</xsl:template>

 

 

<xsl:template name="reverse">

    <xsl:param name="input"/>

    <xsl:variable name="len" select="string-length($input)"/>

    <xsl:choose>

    <!-- Strings of length less than 2 are trivial to reverse -->

        <xsl:when test="$len &lt; 2">

            <xsl:value-of select="$input"/>

        </xsl:when>

    <!-- Strings of length 2 are also trivial to reverse -->

        <xsl:when test="$len = 2">

            <xsl:value-of select="substring($input,2,1)"/>

            <xsl:value-of select="substring($input,1,1)"/>

        </xsl:when>

        <xsl:otherwise>

    <!-- Swap the recursive application of this template to

    the first half and second half of input -->

            <xsl:variable name="mid" select="floor($len div 2)"/>

            <xsl:call-template name="reverse">

                <xsl:with-param name="input" select="substring($input,$mid+1,$mid+1)"/>

            </xsl:call-template>

            <xsl:call-template name="reverse">

                <xsl:with-param name="input" select="substring($input,1,$mid)"/>

            </xsl:call-template>

        </xsl:otherwise>

    </xsl:choose>

</xsl:template>

</xsl:stylesheet>