This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: highlighting words/phrases in xml document?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]