3

Here is the xml:

<?xml version="1.0" encoding="UTF-8"?>
    <file>
        <text>
            <p>
               <sentence>I bought kiwi at the grocery store.</sentence>
               <sentence>I also bought bananas at the store.</sentence>
               <sentence>Then, I bought a basket at another store.</sentence>
            </p>
            <p>
                <sentence>You bought kiwi at the grocery store.</sentence>
                <sentence>You also bought bananas at the store.</sentence>
                <sentence>Then, You bought a basket at another store.</sentence>
            </p>
        </text>
    </file>

And here is the XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="sentence">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
        <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="file/text/p/sentence[contains(.,$search)]"/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

I need to highlight the $search word in the results like this when $search="kiwi":

I bought <mark>kiwi</mark> at the grocery store.

You bought <mark>kiwi</mark> at the grocery store.

Please help!

Steve DEU
  • 167
  • 1
  • 13
  • In your sample the search word `kiwi` is contained on its own (minus white space) in the element `fruit` so it is already marked up and you could simply transform any `fruit` element matching the search term if that is always the case. Or can the search term be anywhere in a `sentence` element? – Martin Honnen Sep 01 '17 at 20:06
  • My bad! There shouldn't be inside . I modified the XML. – Steve DEU Sep 01 '17 at 20:09
  • 1
    It's relatively easy to highlight the **string** "kiwi". It's not so easy to highlight the **word** "kiwi" (one needs to define a *word*). – michael.hor257k Sep 01 '17 at 20:39
  • I guess I should say highlighting the string since the search term can be phrases or incomplete word. – Steve DEU Sep 01 '17 at 20:46

2 Answers2

5

To highlight all occurrences of the search string, change this:

<xsl:template match="sentence">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

to:

<xsl:template match="sentence"> 
    <p> 
        <xsl:call-template name="hilite">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="search-string" select="$search"/>
        </xsl:call-template>
    </p> 
</xsl:template> 

<xsl:template name="hilite">
    <xsl:param name="text"/>
    <xsl:param name="search-string"/>
    <xsl:choose>
        <xsl:when test="contains($text, $search-string)">
            <xsl:value-of select="substring-before($text, $search-string)"/>
            <mark>
                <xsl:value-of select="$search-string"/>
            </mark>
            <xsl:call-template name="hilite">
                <xsl:with-param name="text" select="substring-after($text, $search-string)"/>
                <xsl:with-param name="search-string" select="$search-string"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

have a look at the following example that uses XPath 3.0 fn:analyze-string() which is better than xsl:analyze-string in my opinion and works well with Saxon 9.8 HE. I also tried michael's solution first, however it did not work properly in my use case.

It should work on child nodes as well without breaking the hierarchy or loosing tails.

   <xsl:template name="highlight">
        <xsl:param name="element"/>
        <xsl:param name="search" as="xs:string"/>
        <xsl:param name="flags" required="no" select="'im'" as="xs:string"/>

        <xsl:choose>
            <xsl:when test="empty($search) or $search eq ''">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$element/node()">
                    <xsl:choose>
                        <xsl:when test=". instance of text()">
                            <xsl:for-each select="fn:analyze-string(., $search, $flags)/*">
                                <xsl:choose>
                                    <xsl:when test="local-name(.) eq 'non-match'">
                                        <xsl:value-of select="./text()"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <span class="highlighted">
                                            <xsl:value-of select="./text()"/>
                                        </span>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="highlight">
                                <xsl:with-param name="element" select="."/>
                                <xsl:with-param name="search" select="$search"/>
                                <xsl:with-param name="flags" select="$flags"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

for xQuery alone, see:

xquery version "3.1";

module namespace functions = 'https://bdo.badw.de/functions';

declare function functions:highlight($element, $search) {
    element {local-name($element)} {
        for $e in $element/node()
        return
            if ($e instance of text() = true()) then
                for $match in fn:analyze-string($e, $search, 'im')/*
                return
                    if (local-name($match) eq 'non-match') then
                        $match/text()
                    else
                        <span class="highlighted">{$match/text()}</span>
            else
                functions:highlight($e, $search)
    }
};
meistermuh
  • 393
  • 3
  • 11