This section provides information about the templates that are defined and used in the Search Core Results XSLT.
To begin a review of the Search Core Results XSLT, examine the section of the code where the transformation is initiated. This section is located at the end of the code; you can find it by searching for the following tag:
The following syntax defines a template and associates it with the root node of the XML:
<All_Results>
…
</All_Results>
The full XSLT for this template follows:
<xsl:template match="/">
<xsl:variable name="Rows" select="/All_Results/Result" />
<xsl:variable name="RowCount" select="count($Rows)" />
<xsl:variable name="IsEmpty" select="$RowCount = 0" />
<xsl:if test="$AlertMeLink">
<input type="hidden" name="P_Query" />
<input type="hidden" name="P_LastNotificationTime" />
</xsl:if>
<xsl:choose>
<xsl:when test="$IsNoKeyword = 'True'" >
<xsl:call-template name="dvt_1.noKeyword" />
</xsl:when>
<xsl:when test="$IsEmpty">
<xsl:call-template name="dvt_1.empty" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="dvt_1.body"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template> The root template does not transform any of the results XML data. Its primary function is to determine which template should be applied next, based on conditional tests on the search results XML data.
The conditional tests in the XSLT try to determine which of the following search results Web Part scenarios applies:
-
Search Core Results Web Part contains search results.
-
Search Core Results Web Part contains no search results, because one of the following is true:
The XSLT first tests for the following conditions:
If you navigate directly to the results page without specifying any search words, or if the Web Part was configured to use a fixed keyword query but no keyword was specified, the first condition will return true. If the condition returns true, then the XSL template applied for the main body of the Search Core Results Web Part is the dvt_1.noKeyword template. The following figure shows how this template appears in the results page:
To modify the appearance, in the Search Core Results Web Part XSLT, search for this template's opening tag:
<xsl:template name="dvt_1.noKeyword">
and modify the contents.
The second condition returns true if the search query entered by the user returned no results. For this case, the template applied to the main body of the Search Core Results Web Part is the dvt_1.empty template. The following figure shows how this template appears in the results page:
To modify the text or action links that are displayed, search for this template's opening tag in the XSLT:
<xsl:template name="dvt_1.empty">
and then modify the contents.
If search results are returned—because neither of the conditions described in the previous section return true—then the template specified in the <xsl:otherwise> tag of this template's XSLT is applied to the main body of the Search Core Results Web Part.
To view the XSLT for this template, search the code for the following:
<xls:template name="dvt_1.body">
The transform code in this template is used primarily for the header shown with the search results; you would modify this template for any changes you want to make to the header. The following figure shows the default header:
Toward the end of this template's code, after the search results header transform code, you'll find the following line of XSLT:
This line of code indicates that the XSL transform should continue on, and apply templates to child nodes where a match is found. If you examine the closing tag for the dvt_1.body template, you'll see that the next line of XSLT is the following:
<xsl:template match="Result">
This code defines the template to associate with the Result node, and contains the XSLT to build the display of the search results, which includes the following elements for each result:
-
Icon/image
-
Title
-
Description
-
Metadata
The default XSLT for the search results metadata includes the values for the Path, Size, Author, and LastModified properties.
Within those sections, you'll notice that the XSLT calls additional templates. These are described in detail in the following sections.
HitHighlighting Template
The HitHighlighting template is responsible for highlighting the search words displayed in the following results sections:
To understand how this XSLT works, you must look at the hithighlighting-related child elements of the <Result></Result> tag in the search results XML data. The following example shows a case where the search words entered by the user were "SharePoint", "Namespace", and "Class":
<hithighlightedsummary>
<c0>SharePoint</c0> <c1>namespace</c1> provides types and members that can be used for working with a <c0>SharePoint</c0> site <ddd /> a top-level <c2>class</c2> that represents a <c0>SharePoint</c0> site and provides access to its collection of subsites <ddd /> <c0>SharePoint</c0> <c1>namespace</c1> and a brief description of each.
</hithighlightedsummary>
<hithighlightedproperties>
<HHTitle>Microsoft.<c0>SharePoint</c0> <c1>Namespace</c1></HHTitle>
<HHUrl>http://msdn.microsoft.com/library/default.asp?url=/library/en- us/spptsdk/html/tsnsMicrosoft<c0>SharePoint</c0>_SV01017995.asp</HHUrl>
</hithighlightedproperties>
The <hithighlightedsummary></hithighlightedsummary>, <HHtitle></HHtitle>, and <HHUrl></HHUrl> elements have child elements embedded in their XML data that act as wrappers for each of the search words found in the XML data.
When there are multiple search words, each word is contained in a different element; however the same element is used for different instances of the same search word.
As shown in the preceding example, the <c0></c0> tag contains the first search word, the <c1></c1> element contains the second search word, and so on.
You can modify the HitHighlighting template to customize how the search words are highlighted in the results. For example, you could change the color of the highlighted text, and use different colors to represent different search words.
The following XSLT code sample demonstrates how to do this:
<xsl:template match="c0">
<b style="color: #ff0033">
<xsl:value-of select="."/>
</b>
</xsl:template>
<xsl:template match="c1">
<b style="color: #990066">
<xsl:value-of select="."/>
</b>
</xsl:template> The highlighted sections of the sample identify the code that was added to the default XSLT code to change the color used to highlight the search words. You can find this section of code in the HitHighlighting template. To locate this template, search for:
<xsl:template name="Hithightlighting">
in the XSLT.
DisplaySize Template
The DisplaySize template is responsible for displaying the Size property returned in the results. This template's XSLT calculates the most appropriate unit to use (selecting from bytes, KB, or MB), and adjusts the displayed size value accordingly.
DisplayString Template
The DisplayString template is a generic template for displaying string values returned in the results' XML data. The default XSLT uses this template to display the Author and LastModified properties.