[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: highlighting words/phrases in xml document?

Subject: RE: highlighting words/phrases in xml document?
From: "Jarno Elovirta" <jarno@xxxxxxxxxxxxxx>
Date: Wed, 20 Jun 2001 08:16:33 +0300
xsl highlight words
Hi!

> what's the (best) approach using XSL to highlight words and
> phrases in an XML
> document -- similar to the highlighting of search results in
> traditional search
> and find?
>
> the phrase and the element inside which to consider the phrase as
> a hit are
> given as parameters.

Not the best solution, but hope this helps you to write the one

[c:\temp]type test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<chapter>
  ...
  <v>And he gathered them together into a place called in the Hebrew tongue
Armageddon.</v>
  <v>And the seventh angel poured out his vial into the air; and there came
a great voice out of the temple of heaven, from the throne, saying, It is
done.</v>
  ...
</chapter>

[c:\temp]type test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
            encoding="ISO-8859-1" />

<xsl:template match="/">
  <xsl:apply-templates select="/" mode="search">
    <xsl:with-param name="phrase" select="'heaven'" />
    <xsl:with-param name="element" select="'v'" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="/" mode="search">
  <xsl:param name="phrase" select="''" />
  <xsl:param name="element" select="''" />
  <xsl:apply-templates select="@*|node()" mode="search">
    <xsl:with-param name="phrase" select="$phrase" />
    <xsl:with-param name="element" select="$element" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="@*|node()" mode="search">
  <xsl:param name="phrase" select="''" />
  <xsl:param name="element" select="''" />
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="search">
      <xsl:with-param name="phrase" select="$phrase" />
      <xsl:with-param name="element" select="$element" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()" mode="search">
  <xsl:param name="phrase" select="''" />
  <xsl:param name="element" select="''" />
  <xsl:choose>
    <xsl:when test="parent::*[1][name() = $element] and contains(.,
$phrase)">
      <xsl:call-template name="highlight">
        <xsl:with-param name="phrase" select="$phrase" />
        <xsl:with-param name="text" select="." />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="." />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="highlight">
  <xsl:param name="phrase" select="''" />
  <xsl:param name="text" select="''" />
  <xsl:choose>
    <xsl:when test="contains($text, $phrase)">
      <xsl:value-of select="substring-before(., $phrase)" />
      <em>
        <xsl:value-of select="$phrase" />
      </em>
      <xsl:call-template name="highlight">
        <xsl:with-param name="phrase" select="$phrase" />
        <xsl:with-param name="text" select="substring-after(., $phrase)" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

[c:\temp]jd.xslt test.xml test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<chapter>
  ...
  <v>And he gathered them together into a place called in the Hebrew tongue
Armageddon.</v>
  <v>And the seventh angel poured out his vial into the air; and there came
a great voice out of the temple of <em>heaven</em>, from the throne, saying,
It is done.</v>
  ...
</chapter>

Cheers,

Sini


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.