如何从XSLT突出显示搜索结果的颜色?

时间:2014-04-09 16:04:19

标签: xml search xslt highlight

我有一个简单的单词widcard param,名为' search'在XSLT 1.0中。 我使用contains函数返回结果。 如何在搜索结果中突出显示此通配符?

这是我的xslt:

<?xml version="1.0" encoding="UTF-8"?>

          

<xsl:param name="search"></xsl:param> 

<xsl:key name="uniquePublicationsGroupHeading" match="organisationUnitDetails/person/publications/publicationsgroup" use="publicationsgroupheading" />

<xsl:template match="/">

    <xsl:call-template name="publicationsYearIndex"></xsl:call-template>
  <xsl:for-each select="//publicationsgroup[generate-id(.)=generate-id(key('uniquePublicationsGroupHeading',publicationsgroupheading))]"> 
        <xsl:sort select="publicationsgroupheading" data-type="number" order="descending"/>

            <h4>Publications - <xsl:value-of select="publicationsgroupheading"/></h4>                
            <ol>                   
                <xsl:for-each select="key('uniquePublicationsGroupHeading',publicationsgroupheading)">   
                    <xsl:for-each select="publicationslist/publicationdetails">

                        <xsl:if test="contains(translate(current(),$uppercase,$lowercase),translate($search,$uppercase,$lowercase))">                              

                      <!--  TODO: NEED TO HIGHLIGHT wildcard param '$search' within current()....-->
                            <li class="margin-bottom"><xsl:value-of select="current()" disable-output-escaping="yes"/></li>

                        </xsl:if>

                    </xsl:for-each>  
                </xsl:for-each>                        
            </ol>               

    </xsl:for-each>       
</xsl:template> 

1 个答案:

答案 0 :(得分:1)

正如Jim所说,编写HTML来突出搜索词完全取决于你。

假设您要将所有搜索字词出现在strong标记中,您需要处理current()以将此标记添加到搜索字词的每个外观中。为此,您可以替换

<xsl:value-of select="current()" disable-output-escaping="yes"/>

使用递归模板调用:

<xsl:call-template name="highlight">
  <xsl:with-param name="string" select="current()"/>
  <xsl:with-param name="term" select="$search"/>
</xsl:call-template>

和替换的模板可能看起来像

<xsl:template name="highlight">
    <xsl:param name="string"/>
    <xsl:param name="term"/>

    <xsl:variable name="before" select="substring-before($string, $term)"/>

    <xsl:choose>
        <xsl:when test="starts-with($string, $term) or string-length($before) &gt; 0">
            <xsl:value-of select="$before"/>
            <!-- write whatever markup to highlight search term occurrence here -->
            <strong><xsl:value-of select="$term"/></strong>
            <xsl:variable name="after" select="substring-after($string, $term)"/>
            <xsl:if test="string-length($after) &gt; 0">
                <xsl:call-template name="highlight">
                    <xsl:with-param name="string" select="$after"/>
                    <xsl:with-param name="term" select="$term"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>