Re: Best way to handle multiple string replacements?

Subject: Re: Best way to handle multiple string replacements?
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Tue, 06 Jun 2000 18:02:37 +0100
Hi Sebastian,

Sebastian Rahtz wrote:
>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

Right, thanks for explaining (and providing the code).  I did briefly think
about doing it this way.  Two objections came to mind.

My first objection was that I thought it would be relatively inefficient if
you have a long piece of text and not very many substitutions to be made
within it.  I could well be wrong on that (I'm no computer scientist) - out
of interest, did you compare the performance of the two stylesheets?

My second objection was that it would get a bit more complicated when you
had substitutions of things that were of arbitrary length (i.e. strings
rather than single characters).  That wasn't a problem in Warren's example,
but I was aiming for reusability.  A look-ahead mechanism could probably
get around that?

These two objections were enough to guide me away from that approach,
fairly or unfairly.

> > 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

True, although the order of events would also be crucial (and implicit)
within your method in the case where more than one character was being

Thanks for highlighting this alternative method - I'm sure that Warren will
find it useful, and it has helped me to uncover the implicit criteria
behind my design decisions.  I would be interested to see the results of
the performance comparison, if you do it.



Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email

 XSL-List info and archive:

Current Thread