Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL - Removing the filename from the path string

I've got a SharePoint problem which I need some help with. I'm creating some custom ItemStyles to format the output of a Content Query Webpart (CQWP) but I need to insert a "view all" button into the output.

View all needs to point to: http://www.site.com/subsite/doclibrary1/Forms/AllItems.aspx

All the individual files in the document library have the link of: http://www.site.com/subsite/doclibrary1/FileName.doc

So what I need is some XSL functions to strip FileName.doc from the end of the string.

I've tried using substring-before($variable, '.') to get rid of the .doc, but I then need to find a way to use substring-after to search for the LAST forward slash in the series and truncate the orphaned filename.

Using @Mads Hansen's post, this is the code which resolved the problem:

Template in ItemStyle.xsl

<xsl:template name="ImpDocs" match="Row[@Style='ImpDocs']" mode="itemstyle">
    <xsl:variable name="SafeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="ViewAllLink">
        <xsl:call-template name="OuterTemplate.getCleanURL">
            <xsl:with-param name="path" select="@LinkUrl"/>
        </xsl:call-template>
    </xsl:variable>
    <div class="DocViewAll">
        <a href="{$ViewAllLink}Forms/AllItems.aspx" title="View all">View All</a>
        <!--Any other code you need for your custom ItemStyle here-->
    </div>
</xsl:template>

Template in ContentQueryMain.xsl

<xsl:template name="OuterTemplate.getCleanURL">
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path,'/')">
            <xsl:value-of select="substring-before($path,'/')" />
            <xsl:text>/</xsl:text>
            <xsl:call-template name="OuterTemplate.getCleanURL">
                <xsl:with-param name="path" select="substring-after($path,'/')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise />
    </xsl:choose>
</xsl:template>
like image 850
MrFidge Avatar asked Oct 08 '10 12:10

MrFidge


People also ask

What does node () do in XSLT?

XSLT current() Function Usually the current node and the context node are the same. However, there is one difference. Look at the following XPath expression: "catalog/cd". This expression selects the <catalog> child nodes of the current node, and then it selects the <cd> child nodes of the <catalog> nodes.

How do you break a line in XSLT?

Include the attribute Method="text" on the xsl:output tag and include newlines in your literal content in the XSL at the appropriate points. If you prefer to keep the source code of your XSL tidy use the entity &#10; where you want a new line.

What does xsl value of select /> mean?

Definition and Usage The <xsl:value-of> element extracts the value of a selected node. The <xsl:value-of> element can be used to select the value of an XML element and add it to the output.

What is xsl Strip space?

Used at the top level of the stylesheet to define elements in the source document for which whitespace nodes are insignificant and should be removed from the tree before processing.


2 Answers

Executing this stylesheet produces: http://www.site.com/subsite/doclibrary1/

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
<xsl:template match="/">

    <xsl:call-template name="getURL">
        <xsl:with-param name="path">http://www.site.com/subsite/doclibrary1/FileName.doc</xsl:with-param>
    </xsl:call-template>
</xsl:template>

    <xsl:template name="getURL">
        <xsl:param name="path" />
        <xsl:choose>
            <xsl:when test="contains($path,'/')">
                <xsl:value-of select="substring-before($path,'/')" />
                <xsl:text>/</xsl:text>
                <xsl:call-template name="getURL">
                    <xsl:with-param name="path" select="substring-after($path,'/')" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise />
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

The getURL template makes a recursive call to itself when there are "/" characters in the string. While there are still "/" characters, it spits out the values before the slash, and then invokes itself. When it reaches the last one, it stops.

like image 197
Mads Hansen Avatar answered Sep 20 '22 07:09

Mads Hansen


The given solutions are not able to handle url's without filename and extension at the end (Path to folder)

I changed the ideas above to include this aswell...

   <xsl:template name="getPath"> 
        <xsl:param name="url" /> 
        <xsl:choose> 
        <xsl:when test="contains($url,'/')"> 
                <xsl:value-of select="substring-before($url,'/')" /> 
                <xsl:text>/</xsl:text> 
                <xsl:call-template name="getPath"> 
                    <xsl:with-param name="url" select="substring-after($url,'/')" /> 
                </xsl:call-template> 
        </xsl:when > 
        <xsl:otherwise>
            <xsl:if test="not(contains($url,'.'))"> 
            <xsl:value-of select="$url" /> 
            </xsl:if>
        </xsl:otherwise> 
    </xsl:choose>
</xsl:template> 

Btw. Why does MS still not support XSLT 2.0!, i saw people complainin bout that back in 2007 -.-'

like image 44
Sven Mawby Avatar answered Sep 22 '22 07:09

Sven Mawby