xsl-list
[Top] [All Lists]

RE: [xsl] Highlighting words/phrases

2006-07-27 14:54:46
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(_at_)yahoo(_dot_)com(_dot_)br 
[mailto:yguaba(_at_)yahoo(_dot_)com(_dot_)br] 
Sent: 27 July 2006 22:27
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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/ 
 



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>