XSL - remove a file name from a path string

I have a SharePoint problem that I need help with. I am creating some ItemStyles custom elements to format the output of the content request web part (CQWP), but I need to insert the "view all" button in the output.

Show everything should point to: http://www.site.com/subsite/doclibrary1/Forms/AllItems.aspx

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

So, I need some XSL functions to remove FileName.doc from the end of the line.

I tried using substring-before ($ variable, '.') To get rid of .doc, but then I need to find a way to use the substring after finding the LAST slash in the series and truncate the orphaned file name.

Using the @Mads Hansen post, this is the code that resolved the issue:

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> 
+4
source share
5 answers

Running this style sheet creates: 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 pattern makes a recursive call to itself when the string contains "/" characters. Although there are still "/" characters, it splashes out the values ​​before the slash, and then calls itself. When he reaches the last, he stops.

+4
source

These solutions cannot handle the URL without the file name and extension at the end (folder path)

I modified the ideas above to include this as well ...

  <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 MS still does not support XSLT 2.0 !, I saw people complaining back in 2007. -

+2
source

If you are using XSLT 2.0 (or, more specifically, XPath 2.0), then you should be able to use the replace function using a regular expression to capture the substring before the last "/": http://www.w3.org/TR/xpath- functions / # func-replace

Unfortunately, "replace" does not exist in XSLT 1.0, so it depends on which XSLT processor you use, whether it will work for you.

0
source

This style sheet:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="url"> <xsl:variable name="vReverseUrl"> <xsl:call-template name="reverse"/> </xsl:variable> <xsl:call-template name="reverse"> <xsl:with-param name="pString" select="substring-after($vReverseUrl,'/')"/> </xsl:call-template> </xsl:template> <xsl:template name="reverse"> <xsl:param name="pString" select="."/> <xsl:if test="$pString"> <xsl:call-template name="reverse"> <xsl:with-param name="pString" select="substring($pString,2)"/> </xsl:call-template> <xsl:value-of select="substring($pString,1,1)"/> </xsl:if> </xsl:template> </xsl:stylesheet> 

With this input:

 <url>http://www.site.com/subsite/doclibrary1/FileName.doc</url> 

Conclusion:

 http://www.site.com/subsite/doclibrary1 

One line of XPath 2.0:

 string-join(tokenize(url,'/')[position()!=last()],'/') 
0
source

See my answer to this question and use the same technique (@Alejandro's answer essentially copies this).

0
source

All Articles