[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Match DEF in 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|