Re[3]: [xsl] Highlighting words/phrases

Subject: Re[3]: [xsl] Highlighting words/phrases
From: Cindy Girard <clm6u@xxxxxxxxxxxx>
Date: Mon, 28 Aug 2006 12:44:10 -0400
Hi.

I thought I had everything working wonderfully - Alexander Johannesen's
highligher template helped alot - thanks, Alexander!

However, I'm having mixed-content problems.

I originally called the highlighter template from the element templates (p, for
example), but that either lost the child element tags, or put the tags
and content in the wrong place. Then I tried calling it from the template
matching text() (which I thought was working last night, but is no
longer working today), and now it doesn't catch the word at all.

Is there a solution I'm not seeing?

Thanks,
Cindy



========================================================================================

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE xsl:stylesheet>

<xsl:stylesheet 
        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
        xmlns:doc="http://xsltsl.org/xsl/documentation/1.0";
        xmlns:str="http://xsltsl.org/string";
        extension-element-prefixes="doc str"
        version='1.1'>
        
<xsl:import href="string.xsl"/> 

<xsl:output 
     method='xml' 
     indent='yes' 
     encoding='iso-8859-1'
     doctype-system='http://jefferson.village.virginia.edu/utc_dtd/utc.dtd'/>


<xsl:param name="keyword" select="''"/>

<!-- This copies one xml file to a copy of the same xml file. Note that a processing
instruction in a declaration subset will not be copied.  -->
<xsl:template match='/'>
        <xsl:apply-templates select='*|@*|comment()|processing-instruction()|text()'/>
</xsl:template>


<xsl:template name="highlighter">
   <xsl:param name="text"/>
   <xsl:param name="what"/>
   <xsl:variable name="test-text">
      <xsl:call-template name="str:to-lower">
         <xsl:with-param name="text" select="$text" />
      </xsl:call-template>
   </xsl:variable>
   <xsl:variable name="test-what">
      <xsl:call-template name="str:to-lower">
         <xsl:with-param name="text" select="$what" />
      </xsl:call-template>
   </xsl:variable>
   <xsl:variable name="uc-what">
      <xsl:call-template name="str:to-upper">
         <xsl:with-param name="text" select="$what" />
      </xsl:call-template>
   </xsl:variable>
   <xsl:choose>
      <xsl:when test="contains($test-text, $test-what)">
         <xsl:variable name="before" select="substring-before($test-text, $test-what)"/>
         <xsl:variable name="after" select="substring-after($test-text, $test-what)"/>
         <xsl:variable name="real-before" select="substring($text, 1, string-length($before))"/>
         <xsl:variable name="real-what" select="substring($text, string-length($before) + 1, string-length($what))"/>
         <xsl:variable name="real-after" select="substring($text, string-length($before) + string-length($what) + 1)"/>
         <xsl:value-of select="$real-before"/>
         <highlight><xsl:value-of select="$real-what"/></highlight>
         <xsl:call-template name="highlighter">
            <xsl:with-param name="text" select="$real-after"/>
            <xsl:with-param name="what" select="$what"/>
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="$text"/>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="text()">
  <xsl:copy>
         <xsl:call-template name="highlighter">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="what" select="$keyword"/>
         </xsl:call-template>
  </xsl:copy>
</xsl:template>

<xsl:template match="*|@*|comment()|processing-instruction()">
  <xsl:copy>
    <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>

========================================================================================

sample xml:

<tei.2 type="notices" id="noar01a"><teiheader>...</teiheader>
<text><body><div1 type="Article"><head>A New Story by Mrs. Stowe</head>
<p>Week after next we propose to commence in the <hi rend="italics">Era</hi>, the
publication of a new story by Mrs. H. B. Stowe, the title of which will be,
"<hi rend="fullsmcaps">UNCLE TOM'S CABIN</hi>, <hi rend="smcaps">OR THE</hi>
 <hi rend="fullsmcaps">MAN</hi> <hi rend="smcaps">THAT WAS A</hi>
 <hi rend="fullsmcaps">THING</hi>."  It will probably be of the length of the Tale by Mrs.
Southworth, entitled Retribution.</p>
<p>Mrs. Stowe is one of the most gifted and popular of American writers.  We announce her
story in advance, that none of our subscribers, may lose the beginning of it, and that those who
desire to read the production as it may appear in successive numbers of the
<hi rend="italics">Era</hi>, may send us their names in season.</p></div1></body></text></tei.2>

Current Thread