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

Re: Match DEF in ABCDEFGHIJ

Subject: Re: Match DEF in ABCDEFGHIJ
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 17 Jul 2003 09:02:46 -0600 (MDT)
abcdefghij
Karl J. Stubsjoen wrote:
> I'd like to match DEF in ABCDEFGHIJ...  then, I'd like to wrap some special
> HTML code around it "<strong>".

XPath provides the functions contains(), substring-before(), and 
substring-after() which you will find quite helpful. 
To highlight the first occurrence of DEF:

<xsl:variable name="substringToHighlight" select="'DEF'"/>
<xsl:if test="contains(.,$substringToHighlight)">
  <xsl:value-of select="substring-before(.,$substringToHighlight)"/>
  <strong>
    <xsl:value-of select="$substringToHighlight"/>
  </strong>
  <xsl:value-of select="substring-after(.,$substringToHighlight)"/>
</xsl:if>

If you want to highlight all occurrences of DEF, you can turn this
 into a recursive template that feeds the substring-after(...) part
to its next invocation, rather than adding it to the result tree.
If your XSLT processor detects tail recursion and optimizes for it,
it should be safe and efficient (sadly, most processors don't).

> <xsl:template name="HighlightMatches">
>   <xsl:with-param name="c" select="current()"/>
>   <xsl:with-param name="match"/>

You would use xsl:param here, not xsl:with-param.
Instead of current() you probably mean ".", but this will
work better:

<xsl:template name="HightlightMatches">
  <xsl;param name="stringToSearchIn"/>
  <xsl:param name="substringToHighlight"/>
  <xsl:choose>
    <xsl:when test="contains($stringToSearchIn, $substringToHighlight">
      <xsl:value-of select="substring-before($stringToSearchIn, $substringToHighlight)"/>
      <strong>
        <xsl:value-of select="$substringToHighlight"/>
      </strong>
      <xsl:call-template name="HighlightMatches">
        <xsl:with-param name="stringToSearchIn" select="substring-after($stringToSearchIn, $substringToHighlight")"/>
        <xsl:with-param name="substringToHighlight" select="$substringToHighlight"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring-after($stringToSearchIn, $substringToHighlight)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


then you can invoke it like

      <xsl:call-template name="HighlightMatches">
        <xsl:with-param name="stringToSearchIn" select="."/>
        <xsl:with-param name="substringToHighlight" select="'DEF'"/>
      </xsl:call-template>

all untested but should be very close if not 100% correct


-Mike

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.