[xsl] replacing ALL chars in a string in a SINGLE shot

Subject: [xsl] replacing ALL chars in a string in a SINGLE shot
From: "Pramodh Peddi" <peddip@xxxxxxxxxxxxxxxx>
Date: Wed, 14 Jan 2004 11:37:40 -0500
Hi,
Do we have any way to replace ALL different characters in String with other
characters in ONE SINGLE shot?
Example: I have a String "&#174; &#8482;" and the resulting String should be
"&amp;#174; &amp;#8482;". Which means '&#174;' should be replaced by
'&amp;#174;', '&#8482;' should be replaced by '&amp;#8482;'.

Below is what I am doing right now. It is working, but I am happening to
call the template once for each string replacement. Thus, it becomes slow. I
want to know if there is any way we can replace ALL the strings in a SINGLE
call:
          <description>
          <xsl:text
disable-output-escaping="yes"><![CDATA[<![CDATA[]]></xsl:text>
            <xsl:variable name="repregdesc">
            <xsl:call-template name="do-replace">
              <xsl:with-param name="text">
                <xsl:copy-of
select="normalize-space(translate(DESCRIPTION,'ý',' '))"/>
              </xsl:with-param>
              <xsl:with-param name="replace">&#174;</xsl:with-param>
              <xsl:with-param name="by">
                <xsl:text
disable-output-escaping="yes">&amp;#174;</xsl:text>
              </xsl:with-param>
            </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="do-replace">
              <xsl:with-param name="text">
                <xsl:copy-of select="$repregdesc"/>
              </xsl:with-param>
              <xsl:with-param name="replace">
                &#8482;
              </xsl:with-param>
              <xsl:with-param name="by">
                <xsl:text
disable-output-escaping="yes">&amp;#8482;</xsl:text>
              </xsl:with-param>
            </xsl:call-template>
            <xsl:text
disable-output-escaping="yes"><![CDATA[]]]]>></xsl:text>
          </description>

and the do-replace template is as follows:

  <xsl:template name="do-replace">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:copy-of select="substring-before($text,$replace)"/><xsl:copy-of
select="$by"/><xsl:call-template name="do-replace">
        <xsl:with-param name="text"><xsl:copy-of
select="substring-after($text,$replace)"/></xsl:with-param>
        <xsl:with-param name="replace" select="$replace"/>
        <xsl:with-param name="by"><xsl:copy-of
select="$by"/></xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Any help would be greatly appreciated.

Thanks,

Pramodh.


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


Current Thread