You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Pramodh Peddi <pe...@contextmedia.com> on 2004/01/14 17:35:36 UTC
replacing ALL chars in a string while transforming
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 "® ™" and the resulting String should be
"&#174; &#8482;". Which means '®' should be replaced by
'&#174;', '™' should be replaced by '&#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">®</xsl:with-param>
<xsl:with-param name="by">
<xsl:text
disable-output-escaping="yes">&#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">
™
</xsl:with-param>
<xsl:with-param name="by">
<xsl:text
disable-output-escaping="yes">&#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.
Re: replacing ALL chars in a string while transforming
Posted by Santiago Pericas-Geertsen <Sa...@Sun.COM>.
Pramodh,
Have you considered using an external Java function? Operations such as
these are typically easier and perform better if written in Java. See
[1] for more information.
-- Santiago
[1] http://xml.apache.org/xalan-j/extensions.html#ext-functions
On Wed, 2004-01-14 at 11:35, Pramodh Peddi wrote:
> 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 "® ™" and the resulting String should be
> "&#174; &#8482;". Which means '®' should be replaced by
> '&#174;', '™' should be replaced by '&#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">®</xsl:with-param>
> <xsl:with-param name="by">
> <xsl:text
> disable-output-escaping="yes">&#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">
> ™
> </xsl:with-param>
> <xsl:with-param name="by">
> <xsl:text
> disable-output-escaping="yes">&#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.
>