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

Subject: Re[4]: [xsl] Highlighting words/phrases
From: Cindy Girard <clm6u@xxxxxxxxxxxx>
Date: Mon, 28 Aug 2006 16:37:56 -0400
I figured it out - the xsl:copy command in the text() template was
mucking up the works - I took it out, and it works fine.

CG> Hi.

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

CG> However, I'm having mixed-content problems.

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

CG> Is there a solution I'm not seeing?

CG> Thanks,
CG> Cindy

CG> ========================================================================================

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

CG> <!DOCTYPE xsl:stylesheet>

CG> <xsl:stylesheet 
CG>         xmlns:xsl=''
CG>         xmlns:doc="";
CG>         xmlns:str="";
CG>         extension-element-prefixes="doc str"
CG>         version='1.1'>
CG> <xsl:import href="string.xsl"/> 

CG> <xsl:output 
CG>      method='xml' 
CG>      indent='yes' 
CG>      encoding='iso-8859-1'
CG> doctype-system=''/>

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

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

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

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

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

CG> </xsl:stylesheet>

CG> ========================================================================================

CG> sample xml:

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

- Cindy 

Current Thread