Question

I have a simple one word widcard param called 'search' in XSLT 1.0. I use the contains function to return the results . How do I highlight this wildcard within the search results?

Here is my 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> 

Was it helpful?

Solution

As Jim noted, writing HTML to highlight search term is totally up to you.

Assuming you want to put all search term occurrences into strong tags, you'll need to process current() to add this markup to every appearance of search term. To do it you can replace

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

with recursive template call:

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

and template which does replacing might look like

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top