XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 20 Jan 2009 06:07 PM
Hi All,

I want to implement multiple strings 'search and replace' function in XSLT. I have written a code to search and replace two string values. I am implementing recursion, because I want to replace all the occurrences of the searched element in the input string. But, for my code, I am getting an infinite:loop stack overflow error in Stylus studio. The code works fine when I replace only one element instead of two. I even tried chaging the settings, recursion depth, stack size and nothing worked. Can any one of you please tell me where I am wrong in my code? I am attaching my code for reference. Thanks in advance.





DocumentString_Replace.txt
String_Replace sample code

Postnext
Minollo I.Subject: infinite loop error when trying to do multiple search/replace
Author: Minollo I.
Date: 20 Jan 2009 08:01 PM
Originally Posted: 20 Jan 2009 08:00 PM
I didn't try it, but I think the problem is that you are not recursively passing find2/replace2 when you hit find/replace (and you are not passing find/replace when you hit find2/replace2); which means that if there is a match you will call contains specifying an empty string as first argument eventually. Changing it into the following should work:
<xsl:when test="contains($string,$find)">
<xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>

</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>

BTW, setting a breakpoint in the template and checking the variable values should help detecting those kind of problems.

Postnext
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 21 Jan 2009 11:43 AM
Minollo,
Thank you. The new change worked. I am pretty new to XSLT. But, I have a new problem in the output now.
For example, If we take a string:
abab
When i give this sample input: find=a, replace=x, find2=b replace2=y
The output from my code is : xbxy. (Whereas the output should be (xyxy))

The logic is working as, the first find and replace is finding all the occurrences of the element in the entire string and replacing them, but the second find/replace is just changing all the other starting from the last replace done by the first find/replace. What change do I need to do to make the output look like xyxy. Your help is greatly appreciated. Many Thanks in advance

Postnext
Minollo I.Subject: infinite loop error when trying to do multiple search/replace
Author: Minollo I.
Date: 21 Jan 2009 11:54 AM
Yeah, the way the double search/replace is setup it won't work; you always look for the first match first, and if you find it you skip anything in the middle.
An easy way to fix it is to change the first matching condition with this, giving a chance to find/replace also the second match:
<xsl:when test="contains($string,$find)">
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-before($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
<xsl:value-of select="$replace"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>

Postnext
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 21 Jan 2009 12:46 PM
But now, second find/replace is not done at all by this change. Only the first find/replace is done.

Postnext
Minollo I.Subject: infinite loop error when trying to do multiple search/replace
Author: Minollo I.
Date: 21 Jan 2009 12:50 PM
Maybe you misunderstood the suggested change? The whole choose block would look like this:
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-before($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
<xsl:value-of select="$replace"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>

Postnext
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 21 Jan 2009 01:08 PM
Hey, I actually missed a line while doing it. Thank you so much. It works great.

Postnext
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 22 Jan 2009 10:56 AM
Hey Minollo,

When I tried to reproduce the code for three find/replace then again I am running into the infinite loop error. Below see my code below, did I do something wrong?
<xsl:template name="replace_string">
<xsl:param name="string"/>
<xsl:param name="find"/>
<xsl:param name="replace"/>
<xsl:param name="find2"/>
<xsl:param name="replace2"/>
<xsl:param name="find3"/>
<xsl:param name="replace3"/>
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-before($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
<xsl:with-param name="find3" select="$find3"/>
<xsl:with-param name="replace3" select="$replace3"/>
</xsl:call-template>
<xsl:value-of select="$replace"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
<xsl:with-param name="find3" select="$find3"/>
<xsl:with-param name="replace3" select="$replace3"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-before($string,$find2)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
<xsl:with-param name="find3" select="$find3"/>
<xsl:with-param name="replace3" select="$replace3"/>
</xsl:call-template>
<xsl:value-of select="$replace2"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
<xsl:with-param name="find3" select="$find3"/>
<xsl:with-param name="replace3" select="$replace3"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find3)">
<xsl:value-of select="concat(substring-before($string,$find3),$replace3)"/>
<xsl:call-template name="replace_string">
<xsl:with-param name="string" select="substring-after($string,$find3)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
<xsl:with-param name="find3" select="$find3"/>
<xsl:with-param name="replace3" select="$replace3"/>
</xsl:call-template>
</xsl:when>

Posttop
Riya PillaiSubject: infinite loop error when trying to do multiple search/replace
Author: Riya Pillai
Date: 27 Jan 2009 09:44 AM
Can anyone help me with this issue please... It is very urgent. Thanks a lot in advance.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.