【问题标题】:How to color highlight search results from XSLT?如何为 XSLT 的搜索结果着色?
【发布时间】:2014-05-22 23:31:03
【问题描述】:

我在 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> 

【问题讨论】:

  • XSLT 只是将输入的 XML 转换为 HTML 的程序。突出显示将通过更改您输出的 HTML 来处理,因此这与 XSLT 本身几乎没有关系。如果你手动编写 HTML 代码,你会怎么做?将其应用于您正在生成的 HTML。

标签: xml search xslt highlight


【解决方案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>

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多