[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Highlighting words/phrases


Subject: RE: [xsl] Highlighting words/phrases
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 27 Jul 2006 22:54:21 +0100

If you change your code so that it makes a recursive call on "highlight" in
place of the call on "search-and-replace" (whose function is unclear from
your description), then it will highlight all the occurrences.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: yguaba@xxxxxxxxxxxx [mailto:yguaba@xxxxxxxxxxxx] 
> Sent: 27 July 2006 22:27
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Highlighting words/phrases
> 
> Dear all,
> 
> I am using XSLT to output search results. The source XML 
> files can be very roughly exemplified thus:
> 
> <root>
> 	<keywords>
> 		<keyword>term</keyword>
> 		<keyword>another search term</keyword>
> 	</keywords>
> 	<document number="x">
> 		<contents>sample text</contents>
> 	</document>
> 	<document number="y">
> 		<contents>another sample</contents>
> 	</document>
> </root>
> 
> Basically, I want to be able to "highlight" the key words and 
> phrases ("keyword" elements) by enclosing each of them 
> within, say, a "span" 
> tag with a certain "class" attribute (in order to style these 
> bits via CSS).
> 
> My first idea was to use some kind of a search/replace routine, like
> so:
> 
> <xsl:template name="highlight">
> 	<xsl:param name="input"/>
> 	<xsl:param name="search-string"/>
> 	<!-- CSS class -->
> 	<xsl:param name="class-name"/>
> 	<xsl:choose>
> 		<xsl:when test="$search-string and 
> contains($input, $search- string)">
> 			<xsl:value-of 
> select="substring-before($input, $search-string)"/>
> 				<span class="{$class-name}">
> 					<xsl:value-of 
> select="$search-string"/>
> 				</span>
> 				<xsl:call-template 
> name="search-and-replace">
> 					<xsl:with-param 
> name="input" select="substring-after($input, $search-string)"/>
> 					<xsl:with-param 
> name="search-string" select="$search-string"/>
> 					<xsl:with-param 
> name="class-name" select="$class-name"/>
> 				</xsl:call-template>
> 		</xsl:when>
> 		<xsl:otherwise>
> 			<xsl:value-of select="$input"/>
> 		</xsl:otherwise>
> 	</xsl:choose>
> </xsl:template>
> 
> The problem is that this would only highlight the first key 
> word or phrase. But I would need to iterate through all the 
> key words using xsl:for-each and highlight them all, which 
> would of course not work with the above template.
> 
> Any suggestions or links to possible solutions would be appreciated.
> 
> Cheers,
> 
> Erik
> 
> 		
> _______________________________________________________
> Novidade no Yahoo! Mail: receba alertas de novas mensagens no 
> seu celular. Registre seu aparelho agora! 
> http://br.mobile.yahoo.com/mailalertas/ 


Current Thread
Keywords