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

Highlighting words/phrases

Subject: Highlighting words/phrases
From: yguaba@xxxxxxxxxxxx
Date: Thu, 27 Jul 2006 18:27:13 -0300
css highlight words
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

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.