Re: [xsl] general string replace problem

Subject: Re: [xsl] general string replace problem
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 7 Jun 2001 18:22:32 +0100
Hi Chris,

> I'm still having problems using Jeni Tennison's general string
> replace template discussed in the FAQ:

Oh dear.  The problem is that the $search parameter in the
replace_strings template is being set through a result tree fragment
rather than through the select attribute.  That means that when it's
used in the predicate later on, it's interpreted as boolean true
rather than as a number.  If you just change that to:

  <xsl:param name="search" select="1" />

it should work better.

However, I'd amend the template in any case to make it more efficient
by passing the node set of foo:search elements rather than an index
into that set (which means you're going off and finding it each time):

<xsl:template name="replace_strings">
  <xsl:param name="input_text" />
  <xsl:param name="search"
      select="document('')/*/foo:string_replacement/foo:search" />
  <xsl:variable name="replaced_text">
    <xsl:call-template name="replace-substring">
      <xsl:with-param name="text" select="$input_text" />
      <xsl:with-param name="from" select="$search[1]/foo:find" />
      <xsl:with-param name="to" select="$search[1]/foo:replace" />
    <xsl:when test="$search[2]">
      <xsl:call-template name="replace_strings">
        <xsl:with-param name="input_text" select="$replaced_text" />
        <xsl:with-param name="search" select="$search[position() > 1]" />
      <xsl:value-of select="$replaced_text" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread