RE: [xsl] Match DEF in ABCDEFGHIJ

Subject: RE: [xsl] Match DEF in ABCDEFGHIJ
From: "Allan Jones" <allan.jones@xxxxxxxxxxxx>
Date: Thu, 17 Jul 2003 16:11:36 +0100
|I'd like to match DEF in ABCDEFGHIJ...  then, I'd like to wrap 
|some special HTML code around it "<strong>". (that was the short of it)
|I'm imagining that I will add a template to my XSLT doc, 
|something maybe called "HighlightMatches", so it might be:
|<xsl:template name="HighlightMatches">
|  <xsl:with-param name="c" select="current()"/>
|  <xsl:with-param name="match"/>
|</xsl:template>
|I'm at the archives/faq looking now... thanks for you help (if 
|i find what I'm looking for i will explain). Karl

Here's a fairly simple solution, although it may not be as sophisticated
as the one you're looking for.

If you have an input document like this:

<?xml version="1.0" encoding="UTF-8"?>
<string>ABCDEFGHIJ</string>

And an xsl like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:template match="/">
		<xsl:element name="output">
			<xsl:call-template name="HighlightMatches">
				<xsl:with-param name="string"
select="string"/>
				<xsl:with-param name="match">
					<xsl:text>DEF</xsl:text>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>
	<xsl:template name="HighlightMatches">
		<xsl:param name="string"/>
		<xsl:param name="match"/>
		<xsl:choose>
			<xsl:when test="contains($string, $match)">
				<xsl:value-of
select="substring-before($string, $match)"/>
				<xsl:element name="strong">
					<xsl:value-of select="$match"/>
				</xsl:element>
				<xsl:call-template
name="HighlightMatches">
					<xsl:with-param name="string"
select="substring-after($string, $match)"/>
					<xsl:with-param name="match"
select="$match"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>

Your output will turn out something like this:

<?xml version="1.0" encoding="utf-8"?>
<output>ABC<strong>DEF</strong>GHIJ</output>

The basis of the solution is in the string matching inside the
HighlightMatches template, and in the recursive call to itself.  Using
the substring-before() and substring-after() functions, you can select
the parts of the string around the match criterion.  All that then
remains is to put the match criterion inside the <strong> element that
you were talking about.

The point of using the xsl:choose block is to provide an exit from the
recursive call - the otherwise provides the exit so that if the string
currently being tested does not contain the match, you'll select the
string instead of doing any processing on it.  Inside the xsl:when,
however, you process the rest of the string after the match condition so
that any further occurences after the first occurrence of the match
string are also picked up.

Hope that helps to get you on the right path for your solution.

Allan Jones



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


Current Thread