步骤 3:创建 XSLT 转换代码

自定义搜索 Web 部件代码将搜索结果转换为 XML 数据,然后将 XSLT 转换应用于 XML,以便将其设置为适于在浏览器中显示的格式。本示例中使用的转换代码是核心搜索结果 Web 部件所使用的 XSLT 转换的修改版。有关详细信息,请参阅企业级搜索核心结果 XSLT 转换

备注

此代码示例指定的 productXSL.xsl 的虚拟路径为 _layouts virtual 目录,该路径将转换为以下物理路径:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\。如果您将 productXSL.xsl 保存在其他位置,则必须更改此路径才能使示例正确运行。

创建 XSLT 转换代码

  1. 在以下路径中创建 productXSL.xsl 文件。

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

    若要编辑文件,可以使用开发工具(如 Visual Studio 2005),还可以使用文本编辑器(如记事本)。

  2. 将以下代码添加到 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>
    

示例:AdventureWorks 搜索示例 XSLT 转换中可以找到 productXSL.xsl XSLT 转换文件的完整代码。

See Also

任务

演练:创建 AdventureWorks 业务数据应用程序示例的 ASP.NET Web 部件

步骤 1:为自定义搜索 Web 部件建立项目

步骤 2:为自定义搜索 Web 部件添加代码

第 4 步:部署自定义搜索 Web 部件

步骤 5:测试搜索 BDC Web 部件