xslt - XSL - Removing the filename from the path string -
i've got sharepoint problem need with. i'm creating custom itemstyles format output of content query webpart (cqwp) need insert "view all" button output.
view needs point to: http://www.site.com/subsite/doclibrary1/forms/allitems.aspx
all individual files in document library have link of: http://www.site.com/subsite/doclibrary1/filename.doc
so need xsl functions strip filename.doc end of string.
i've tried using substring-before($variable, '.') rid of .doc, need find way use substring-after search last forward slash in series , truncate orphaned filename.
using @mads hansen's post, code resolved 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 need 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>
executing 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 recursive call when there "/" characters in string. while there still "/" characters, spits out values before slash, , invokes itself. when reaches last one, stops.
Comments
Post a Comment