Share via


Enterprise Search Core Results XSLT Transformation

You can modify the Search Core Results Web Part XSLT Transformation (XSLT) to change how the search results appear on search results pages. This topic describes the Search Core Results Web Part XSLT, and how you can customize it.

The following scenarios offer examples of when it may be useful for you to modify this code:

  • You want to make minor changes to the display of search results by modifying the existing templates in the default XSLT.

  • You want to make more complex changes to the display of search results by adding new templates to the XSLT.

  • You want to conditionally change the display of certain results, depending on one of the property values that is returned.

  • You need to modify the XSLT to display a property that you added to the default set of properties returned in the search results.

  • You want to modify the display templates to use a custom cascading style sheet class in place of the default cascading style sheet classes.

Modifying the Search Core Results Web Part XSLT

The Search Core Results Web Part XSLT is specified in the Web Part's XSL property. You can view and modify this property from the Search Core Results Web Part tool pane, in the toolpart for the Data Form Web Part. For more information on viewing and modifying this property, see How to: View and Edit the Search Results XSLT Transformation.

Reviewing Search Results XML Data

Before you begin to modify the XSLT for the Search Core Results Web Part, it's helpful to know what the results XML data contains.

The following code sample is a snippet of the search results XML data.

Note

To view this data, follow the steps described in How to: View Search Results XML Data.

<All_Results>
  <Result>
    <id>1</id> 
    <rank>713</rank> 
    <title>Microsoft.SharePoint Namespace</title> 
    <author /> 
    <size>39058</size> 
    <url>https://msdn.microsoft.com/library/default.asp?url=/library/en-     us/spptsdk/html/tsnsMicrosoftSharePoint_SV01017995.asp</url> 
    <description>Microsoft.SharePoint Namespace</description> 
    <sitename> https://msdn.microsoft.com/library</sitename> 
    <collapsingstatus>0</collapsingstatus>
    <hithighlightedsummary>
      <…>
    </hithighlightedsummary>
    <hithighlightedproperties>
      <…>
    </hithighlightedproperties>
    <…>
   </Result>
   <Result>
    <…>
    <…>
   </Result>
</All_Results>  

The elements included in the search results XML data are determined by the following:

  • The display properties of the Search Core Results Web Part.

  • The managed properties specified in the Results Query Options - Selected Columns setting.

Note

For more information on how to change managed properties that are included in the search results, see How to: Change the Properties Returned in the Core Search Results.

Familiarize yourself with the results XML data before you make modifications to the XSLT. By understanding the structure of what is being transformed, you can more easily make your change.

Working with XSL Templates

This section provides information about the templates that are defined and used in the Search Core Results XSLT.

Root Template

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:

<xsl:template match="/">

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:

    • No search query was issued.

    • No relevant results were returned for the search query.

No Search Results Templates

The XSLT first tests for the following conditions:

  • Does a search query exist?

  • Is the search results set empty?

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:

dvt_1.noKeyword XSL template

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:

dvt_1.empty XSL template

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.

Search Results Templates

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:

dvt_1.body XSL template

Toward the end of this template's code, after the search results header transform code, you'll find the following line of XSLT:

<xsl:apply-templates />

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:

  • Title

  • URL

  • Description

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>https://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.

See Also

Tasks

How to: View and Edit the Search Results XSLT Transformation
How to: Change the Properties Returned in the Core Search Results
How to: View Search Results XML Data
How to: Change the Properties Returned in the Core Search Results

Concepts

Customizing Enterprise Search Results