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

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)
string into letters
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

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.