Re: Best way to handle multiple string replacements?

Subject: Re: Best way to handle multiple string replacements?
From: Sebastian Rahtz <sebastian.rahtz@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 6 Jun 2000 16:25:49 +0100 (BST)
Jeni Tennison writes:
 > I'm not sure that I understand the method your proposing.  Do you mean to
 > have:
 > 
 > <foo:characters>_%${}&amp;</foo:characters>
 > 
 > defining the characters to be escaped, and then index into them using
 > substring()? 

no, I mean

 foreach i `break string into letters`
 do
  if $x=lookup($i)
    output $x
  else
    output $i
  fi
 done
 > Not so - the replacements are done in order, and the same order each time
 > (so it is predictable).  As in Warren's original example, all the
 > single-character escaping is done first (by the replace_characters
 > template), and all the string replacements are done second (by the
 > replace_strings template).

yes, thats OK now. but when someone else edits the script next year to
add in a new special character, it is not obvious to the casual reader
that the order of events is crucial

My sample code is appended. I do not do the character lookup, cos
thats fairly straightforward, I just replace " " with "^M" (to make
sure I cannot use translate().

Yes, the recursion is immense, but so what? As I said before, I think
this is not the right way to solve re-encoding problems anyway. I just
ran it with 18000 characters in the text, and it took 3 minutes 44
seconds with Saxon 5.3.2. 

sebastian


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="/">
 <xsl:call-template name="CharByChar">
   <xsl:with-param name="text">
   The cat sat on the mat
   The cat sat on the mat
   The cat sat on the mat
   The cat sat on the mat
 </xsl:with-param>
 </xsl:call-template>
</xsl:template>

<xsl:template name="CharByChar">
 <xsl:param name="text"/>
 <xsl:if test="$text">
   <xsl:variable name="top" select="substring($text,1,1)"/>
   <xsl:variable name="rest" select="substring($text,2)"/>
 <xsl:choose>
  <xsl:when test="$top = ' '">^M</xsl:when>
  <xsl:otherwise><xsl:value-of select="$top"/></xsl:otherwise>
 </xsl:choose>
 <xsl:call-template name="CharByChar">
   <xsl:with-param name="text"><xsl:value-of select="$rest"/></xsl:with-param>
 </xsl:call-template>   
 </xsl:if>
</xsl:template>
</xsl:stylesheet>



Sebastian


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


Current Thread