Share via


Step 3: Create the XSLT Transformation Code

The custom search Web Part code converts the search results to XML data, and then applies an XSLT transformation to the XML to format it for display in the browser. The transformation code used in this sample is a modified version of the XSLT transformation used by the Core Search Results Web Part. For more information, see Enterprise Search Core Results XSLT Transformation.

Note

The virtual path to productXSL.xsl specified by this code sample is the _layouts virtual directory, which translates to the following physical path: \Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS. If you save productXSL.xsl in a different location, you must change the path for the sample to work correctly.

To create the XSLT transformation code

  1. Create the productXSL.xsl file in the following path.

    \Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\

    To edit the file, you can use a development tool such as Visual Studio 2005, or you can use a text editor such as Notepad.

  2. Add the following code to productXSL.xsl.

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#160;">
    
    ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
      <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:choose>
          <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>
    <xsl:template name="dvt_1.empty">
    <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>
    <xsl:template name="dvt_1.body">
    <xsl:apply-templates />
    </xsl:template>
      <xsl:template match="Result">
        <xsl:variable name="id" select="PRODUCTID"/>
        <xsl:variable name="url" select="PATH"/>
        <span class="srch-Title">
            <a href="{$url}" id="{concat('CSR_',$id)}" title="{$url}">
            <xsl:value-of select="PRODUCTNAME"/></a>
          <br/>
        </span>
        <span class="srch-Metadata">
          <xsl:call-template name="DisplayString">
            <xsl:with-param name="str" select="PRODUCTID" />
            <xsl:with-param name="prop">Product ID:</xsl:with-param>
          </xsl:call-template>
          <br/>
        </span>
        <span class="srch-URL">
          <a href="{$url}" id="{concat('CSR_U_',$id)}" title="{$url}">
            <xsl:value-of select="PATH"/>
            </a><br/><br/>
        </span>
      </xsl:template>
      <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>
    </xsl:stylesheet>
    

You can find the complete code for the productXSL.xsl XSLT transformation file in Sample: AdventureWorks Search Sample XSLT Transformation.

See Also

Tasks

Walkthrough: Creating an ASP.NET Web Part for the AdventureWorks Business Data Application Sample
Step 1: Set Up the Project for the Custom Search Web Part
Step 2: Add the Code for the Custom Search Web Part
Step 4: Deploy the Custom Search Web Part
Step 5: Testing the Search BDC Web Part