In Part I we discussed on how to change the style of the results. But this is not enough, what about to show other things in result items? (This is a request from http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2387134&SiteID=1)
Scenario II. Show me the path!
When you search for something, a XML result table will be returned and transformed by XSLT to show the final result. It contains different properties so you can use them,for example:
<item> <title>Hello World!</title> <url>file://superman/opal/myresume.doc</url></item>
So we can get url value from this table, and show it.
How to show the path file://superman/opal ? If I only want the path of the url not the file, "myresume.doc" should be cut off. But XSL is not a good way to process string, luckily some people already showed us some examples and we can use them.
In this example, we used a template in http://xsltsl.sourceforge.net/.
Insert the following template into core result webpart XSL.
<xsl:template name="substring-before-last"> <xsl:param name="text"/> <xsl:param name="chars"/> <xsl:choose> <xsl:when test="string-length($text) = 0"/> <xsl:when test="string-length($chars) = 0"> <xsl:value-of select="$text"/> </xsl:when> <xsl:when test="contains($text, $chars)"> <xsl:call-template name="substring-before-last-aux"> <xsl:with-param name="text" select="$text"/> <xsl:with-param name="chars" select="$chars"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose></xsl:template>
<xsl:template name="substring-before-last-aux"> <xsl:param name="text"/> <xsl:param name="chars"/> <xsl:choose> <xsl:when test="string-length($text) = 0"/> <xsl:when test="contains($text, $chars)"> <xsl:variable name="after"> <xsl:call-template name="substring-before-last-aux"> <xsl:with-param name="text" select="substring-after($text, $chars)"/> <xsl:with-param name="chars" select="$chars"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="substring-before($text, $chars)"/> <xsl:if test="string-length($after) > 0"> <xsl:value-of select="$chars"/> <xsl:copy-of select="$after"/> </xsl:if> </xsl:when> <xsl:otherwise/> </xsl:choose></xsl:template>
This will add a "substring-before-last" template to your XSL. Then search for <xsl:with-param name="str" select="write" />, insert the following string after </xsl:call-template>.
<br/>Path<xsl:variable name="urlpath"> <xsl:call-template name="substring-before-last"> <xsl:with-param name="text" select="url" /> <xsl:with-param name="chars" select="'/'" /> </xsl:call-template></xsl:variable><a href="{$urlpath}" title="{$urlpath}"><xsl:value-of select="$urlpath"/></a>
This means, use substring-before-last to cut strings like file://superman/opal/myresume.doc to file://superman/opal .
This part of XSLT now looks like this:
Apply the setting, you will see the following result:
You can try different things in XSLT, it's very interesting.
It works well, thanks a lot! I did, however, find one bug. I have our external website as one of my content sources, and if a search returns the main website, http://www.example.com then all the path says is:
Path http:
Any ideas on how to keep it from eating the first two /'s?
Thanks a lot!