[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Re: Using XSLT to add markup to a document


Subject: Re: [xsl] Re: Using XSLT to add markup to a document
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 3 Jul 2003 23:53:36 -0700 (PDT)

Here's one solution:

This transformation:

testMultiSearch.xsl:
-------------------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:pat="my:patterns"
  exclude-result-prefixes="pat"
 >
 
 <pat:patterns>
   <pattern>ala</pattern>
   <pattern>lani</pattern>
 </pat:patterns>
 
  <xsl:template match="/">
    <xsl:call-template name="findAndHighLightStrings">
      <xsl:with-param name="pstrTarget" select="/*"/>
      <xsl:with-param name="pPatterns" 
      select="document('')/*/pat:patterns"/>
      <xsl:with-param name="pElemName" select="'b'"/>
    </xsl:call-template>
  
  </xsl:template>
  
  <xsl:template name="findAndHighLightStrings">
    <xsl:param name="pstrTarget"/>
    <xsl:param name="pPatterns" select="/.."/>
    <xsl:param name="pElemName"/>
    
    <xsl:if test="string-length($pstrTarget) > 0">
      <xsl:variable name="vFound">
        <xsl:for-each select="$pPatterns/*">
          <xsl:if test="starts-with($pstrTarget, .)">
          <xsl:value-of select="concat(., '|')"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>
      
       <xsl:variable name="vPatFound" 
        select="substring-before($vFound, '|')"/>

       <xsl:choose>
         <xsl:when test="string($vFound)">
           <xsl:element name="{$pElemName}">
             <xsl:value-of select="$vPatFound"/>
           </xsl:element>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="substring($pstrTarget,1,1)"/>
         </xsl:otherwise>
       </xsl:choose>
       
       <xsl:variable name="vOffset"
        select="(string-length($vPatFound) + 1) 
               * (string-length($vPatFound) > 0) 
               +
                 2 * (string-length($vPatFound) = 0)"/>
       
       <xsl:call-template name="findAndHighLightStrings">
         <xsl:with-param name="pstrTarget"
          select="substring($pstrTarget, $vOffset)"/>
         <xsl:with-param name="pPatterns" select="$pPatterns"/>
         <xsl:with-param name="pElemName" select="$pElemName"/>
       </xsl:call-template>
    </xsl:if>
    
  </xsl:template>


</xsl:stylesheet>


When applied on this source.xml:


<t>alabalanilanica</t>


produces the wanted result:

<b>ala</b>b<b>ala</b>ni<b>lani</b>ca



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL





Dimitre Novatchev wrote:
--------------------------------------------------------------------------------

This is a variation of the "replace all occurences of a string" problem,
which is well documented in Dave Pawson's FAQ.

The new element is that instead of searching for a single string, one must
search for the (next) occurence of any one from a list of strings.

This necessitates stepping through the string one character at a time and
checking if the remaining text that starts at the current character position
does not start with one of the given strings, and if yes, doing the replace
operation.

Some time ago I was asked to help a colleague with exactly this problem and
the solution produced did not use FXSL.

Using FXSL here is convenient, because one can use its generic string
processing templates (e.g. str-foldl or str-map) that process a string one
character at a time. This will be very similar to the processing done to
split a camelCase name into separate words:

http://www.dpawson.co.uk/xsl/sect2/plaintext.html#d6160e507


An added complexity is what to do when one of the search strings is the
start of another -- this must be specified by the person, who requests the
solution. One way is to use the search strings in priority of their order.
Another is to search for the longest string first.

There are also some optimization techniques to minimize the depth of the
recursion and to achieve faster processing:

"Two-stage recursive algorithms in XSLT"
http://www.topxml.com/xsl/articles/recurse/

As I'm rather busy right at this moment, I have to search and find the
solution (this was a month ago) and I'll post it in the next 24 hours.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

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



Current Thread