This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
general string replace problem
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: [xsl] general string replace problem
- From: "Chris Nolte" <nolte at silicon-age dot com>
- Date: Tue, 5 Jun 2001 16:18:36 -0700
- Organization: Silicon Age, Inc.
- Reply-To: xsl-list at lists dot mulberrytech dot com
I'm still having problems using Jeni Tennison's general string replace
template discussed in the FAQ:
http://www.dpawson.co.uk/xsl/stringreplace.html#d44e34517
I must be applying it incorrectly, but I can't find a complete example of
its being used, nor do I see what I'm doing wrong.
My input file is:
<CONTENT>This is input text.</CONTENT>
My stylesheet is: [sorry for the length]
<?xml version='1.0'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:foo='http://www.foo.org'>
<xsl:output method='text' indent='no' />
<xsl:template match='CONTENT'>
<xsl:variable name='test'>
<xsl:call-template name='replace_strings'>
<xsl:with-param name='input_text' select='text()'/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select='$test'/>
</xsl:template>
<foo:string_replacement>
<foo:search>
<foo:find>This</foo:find>
<foo:replace>That</foo:replace>
</foo:search>
<foo:search>
<foo:find>input</foo:find>
<foo:replace>output</foo:replace>
</foo:search>
</foo:string_replacement>
<xsl:template name="replace_strings">
<xsl:param name="input_text" />
<xsl:param name="search">1</xsl:param>
<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="document('')//foo:search[$search]/foo:find" />
<xsl:with-param name="to"
select="document('')//foo:search[$search]/foo:replace" />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$search < count(document('')//foo:search)">
<xsl:call-template name="replace_strings">
<xsl:with-param name="input_text" select="$replaced_text" />
<xsl:with-param name="search" select="$search + 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replaced_text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name='replace-substring'>
<xsl:param name='text'/>
<xsl:param name='from'/>
<xsl:param name='to'/>
<xsl:choose>
<xsl:when test='contains($text, $from)'>
<xsl:value-of select='substring-before($text, $from)'/>
<xsl:copy-of select='$to'/>
<xsl:call-template name='replace-substring'>
<xsl:with-param name='text'
select='substring-after($text, $from)'/>
<xsl:with-param name='from' select='$from'/>
<xsl:with-param name='to' select='$to'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select='$text'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The result I get (using either XT or Saxon from the command line) is:
Thatoutput is output text.
instead of the desired:
That is output text.
I'd be grateful if anyone could show me the error of my ways.
Chris
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list