[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

general string replace problem

Subject: general string replace problem
From: "Chris Nolte" <nolte@xxxxxxxxxxxxxxx>
Date: Tue, 5 Jun 2001 16:18:36 -0700
string replace substring
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 &lt; 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.