Sample: Search Core Results Web Part XSLT

SharePoint 2007

This is the complete XSLT transformation for the sample described in Walkthrough: Add a Tab and Custom Search Page with Enterprise Search Web Parts to the Search Center. You can copy this code into the XSL property for the Search Core Results Web Part. For step-by-step instructions for using this code, see How to: View and Edit the Search Results XSLT Transformation.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#160;">
  <!-- white space in XSL -->
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:param name="ResultsBy" />
  <xsl:param name="ViewByUrl" />
  <xsl:param name="ViewByValue" />
  <xsl:param name="IsNoKeyword" />
  <xsl:param name="IsFixedQuery" />
  <xsl:param name="ShowActionLinks" />
  <xsl:param name="MoreResultsText" />
  <xsl:param name="MoreResultsLink" />
  <xsl:param name="CollapsingStatusLink" />
  <xsl:param name="CollapseDuplicatesText" />
  <xsl:param name="AlertMeLink" />
  <xsl:param name="AlertMeText" />
  <xsl:param name="SrchRSSText" />
  <xsl:param name="SrchRSSLink" />
  <xsl:param name="DisplayDiscoveredDefinition" select="True" />
  <!-- When there is a keyword to issue the search -->
  <xsl:template name="dvt_1.noKeyword">
    <span class="srch-description">
      <xsl:choose>
        <xsl:when test="$IsFixedQuery">      Please set the Fixed Query property for the Web Part.  </xsl:when>
        <xsl:otherwise>      Enter one or more words to search for in the search box.   </xsl:otherwise>
      </xsl:choose>
    </span>
  </xsl:template>
  <!-- When an empty result set is returned from search -->
  <xsl:template name="dvt_1.empty">
    <div class="srch-sort">
      <xsl:if test="$AlertMeLink and $ShowActionLinks">
        <img src="/_layouts/images/bell.gif" border="0" height="9" width="9" />
        <span class="srch-alertme" >
          <a href ="{$AlertMeLink}" id="CSR_AM1" title="{$AlertMeText}">
            <xsl:value-of select="$AlertMeText" />
          </a>
        </span>
      </xsl:if>
      <xsl:if test="string-length($SrchRSSLink) &gt; 0 and $ShowActionLinks">
        <xsl:if test="$AlertMeLink">      |   </xsl:if>
        <span class="ms-rssfeed">
          <a type="application/rss+xml" href ="{$SrchRSSLink}" title="{$SrchRSSText}" id="CSR_SR">
            <xsl:value-of select="$SrchRSSText"/>
          </a>
        </span>
      </xsl:if>
    </div>
    <br/>
    <br/>
    <span class="srch-description" id="CSR_NO_RESULTS">
      No results matching your search were found.    <ol>
        <li>Check your spelling. Are the words in your query spelled correctly?</li>
        <li>Try using synonyms. Maybe what you're looking for uses slightly different words.</li>
        <li>Make your search more general. Try more general terms in place of specific ones.</li>
        <li>Try your search in a different scope. Different scopes can return different results.</li>
      </ol>
    </span>
  </xsl:template>
  <!-- Main body template. Sets the Results view (relevance or date) options. -->
  <xsl:template name="dvt_1.body">
    <div class="srch-results">
      <xsl:if test="$ShowActionLinks">
        <div class="srch-sort">
          <xsl:value-of select="$ResultsBy" />
          <xsl:if test="$ViewByUrl">
            |     <a href ="{$ViewByUrl}" id="CSR_RV" title="{$ViewByValue}">
              <xsl:value-of select="$ViewByValue" />
            </a>
          </xsl:if>
          <xsl:if test="$AlertMeLink">
            |     <img src="/_layouts/images/bell.gif" border="0" height="9" width="9" />    <span class="srch-alertme" >
              <a href ="{$AlertMeLink}" id="CSR_AM2" title="{$AlertMeText}">
                <xsl:value-of select="$AlertMeText" />
              </a>
            </span>
          </xsl:if>
          <xsl:if test="string-length($SrchRSSLink) &gt; 0">
            |     <span class="ms-rssfeed">
              <a type="application/rss+xml" href ="{$SrchRSSLink}" title="{$SrchRSSText}" id="CSR_SR">
                <xsl:value-of select="$SrchRSSText"/>
              </a>
            </span>
          </xsl:if>
        </div>
        <br />
      </xsl:if>
      <xsl:apply-templates />
      <br />
      <br />
    </div>
    <xsl:call-template name="DisplayMoreResultsAnchor" />
  </xsl:template>
  <!-- This template is called for each result -->
  <xsl:template match="Result">
    <xsl:variable name="id" select="id"/>
    <xsl:variable name="url" select="url"/>
    <span class="srch-Title">
      <xsl:value-of select="productname"/>
      <br/>
    </span>
    <div class="srch-Description">
      <xsl:choose>
        <xsl:when test="hithighlightedsummary[. != '']">
          <xsl:call-template name="HitHighlighting">
            <xsl:with-param name="hh" select="hithighlightedsummary" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="description[. != '']">
          <xsl:value-of select="productdescription"/>
        </xsl:when>
      </xsl:choose>
    </div >
    <span class="srch-URL">
      <a href="{$url}" id="{concat('CSR_U_',$id)}" title="{$url}">
        <xsl:choose>
          <xsl:when test="hithighlightedhroperties/HHUrl[. != '']">
            <xsl:call-template name="HitHighlighting">
              <xsl:with-param name="hh" select="hithighlightedhroperties/HHUrl" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="url"/>
          </xsl:otherwise>
        </xsl:choose>
      </a>
    </span>
    <p class="srch-Metadata">
      <xsl:call-template name="DisplayString">
        <xsl:with-param name="str" select="productcategory" />
        <xsl:with-param name="prop">Category:</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="DisplayString">
        <xsl:with-param name="str" select="productmodel" />
        <xsl:with-param name="prop">Model:</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="DisplayString">
        <xsl:with-param name="str" select="productnumber" />
        <xsl:with-param name="prop">Number:</xsl:with-param>
      </xsl:call-template>
    </p>
  </xsl:template>
  <xsl:template name="HitHighlighting">
    <xsl:param name="hh" />
    <xsl:apply-templates select="$hh"/>
  </xsl:template>
  <xsl:template match="ddd">   &#8230;  </xsl:template>
  <xsl:template match="c0">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c1">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c2">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c3">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c4">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c5">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c6">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c7">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c8">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <xsl:template match="c9">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>
  <!-- A generic template to display string with non zero (0) string length (used for author and last-modified time) -->
  <xsl:template name="DisplayString">
    <xsl:param name="str" />
    <xsl:param name="prop" />
    <xsl:if test='string-length($str) &gt; 0'>
      ||  <xsl:value-of select="$prop" />&#160;   <xsl:value-of select="$str" />
    </xsl:if>
  </xsl:template>
  <!-- document collapsing link setup -->
  <xsl:template name="DisplayCollapsingStatusLink">
    <xsl:param name="status"/>
    <xsl:param name="url"/>
    <xsl:if test="$CollapsingStatusLink">
      <xsl:choose>
        <xsl:when test="$status=1">
          <br/>
          <xsl:variable name="CollapsingStatusHref" select="concat(substring-before($CollapsingStatusLink, '$$COLLAPSE_PARAM$$'), 'duplicates:&quot;', $url, '&quot;', substring-after($CollapsingStatusLink, '$$COLLAPSE_PARAM$$'))"/>
          <span class="srch-dup">
            [<a href="{$CollapsingStatusHref}">
              <xsl:value-of select="$CollapseDuplicatesText"/>
            </a>]
          </span>
        </xsl:when>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
  <!-- The "view more results" for fixed query -->
  <xsl:template name="DisplayMoreResultsAnchor">
    <xsl:if test="$MoreResultsLink">
      <a href="{$MoreResultsLink}" id="CSR_MRL">
        <xsl:value-of select="$MoreResultsText"/>
      </a>
    </xsl:if>
  </xsl:template>
  <xsl:template match="All_Results/DiscoveredDefinitions">
    <xsl:variable name="FoundIn" select="DDFoundIn" />
    <xsl:variable name="DDSearchTerm" select="DDSearchTerm" />
    <xsl:if test="$DisplayDiscoveredDefinition = 'True' and string-length($DDSearchTerm) &gt; 0">
      <script language="javascript">      function ToggleDefinitionSelection()      {        var selection = document.getElementById("definitionSelection");        if (selection.style.display == "none")        {          selection.style.display = "inline";        }        else        {          selection.style.display = "none";        }      }    </script>
      <div>
        <a href="#" onclick="ToggleDefinitionSelection(); return false;">
          What people are saying about <b>
            <xsl:value-of select="$DDSearchTerm"/>
          </b>
        </a>
        <div id="definitionSelection" style="display:none;">
          <xsl:for-each select="DDefinitions/DDefinition">
            <br/>
            <br/>
            <xsl:variable name="DDUrl" select="DDUrl" />
            <xsl:value-of select="DDStart"/>
            <b>
              <xsl:value-of select="DDBold"/>
            </b>
            <xsl:value-of select="DDEnd"/>
            <br/>
            <xsl:value-of select="$FoundIn"/>
            <a href="{$DDUrl}">
              <xsl:value-of select="DDTitle"/>
            </a>
          </xsl:for-each>
        </div>
      </div>
    </xsl:if>
  </xsl:template>
  <!-- XSLT transformation starts here -->
  <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>
  <!-- End of Stylesheet -->
</xsl:stylesheet>

See Also

Community Additions

ADD
Show: