Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2009, 12:23 PM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Problem with replacing string in XSLT

Hi All,

I am trying to replace string in XSLT in a .net Application.

Below is the Code how i am trying to replace it :

Template for replacing the String

Code:
 
"<xsl:template name="stringreplace">" 
" <xsl:param name="text"/>" 
" <xsl:param name="replace"/>" 
" <xsl:param name="by"/>" 
" <xsl:choose>" 
" <xsl:when test="contains($text, $replace)">" 
" <xsl:value-of select="substring-before($text,$replace)"/>" 
" <xsl:value-of select="$by"/>" 
" <xsl:call-template name="stringreplace">" 
" <xsl:with-param name="text" select="substring-after($text,$replace)"/>" 
" <xsl:with-param name="replace" select="$replace"/>" 
" <xsl:with-param name="by" select="$by"/>" 
" </xsl:call-template>" 
" </xsl:when>" 
" <xsl:otherwise>" 
" <xsl:value-of select="$text"/>" 
" </xsl:otherwise>" 
" </xsl:choose>" 
"</xsl:template>"
Here i am calling the above template :

Code:
 
<xsl:call-template name="stringreplace">
" <xsl:with-param name="text" select=sAttValue/>
" <xsl:with-param name="replace" select="'</><>'"/>
" <xsl:with-param name="by" select="'|'"/>
" </xsl:call-template>
The value In the variable is sAttValue = " <>3333</><>4444</><>5555</> "

what i am doing is replacing '<></>' with '|' and i am getting remaing string is

<>3333|4444|5555</>.

Now i want want to replace <> and </> with blank value so i vl get

3333|4444|5555

Any Idea How can i do that ?? where can i modify in the template or how to call template again to pass new value for replacing.


Thanks

-Nelly
 
Old September 15th, 2009, 12:57 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can store the result of calling a template in a variable and then you can use that variable when calling the template again, this time with different parameters:
Code:
<xsl:variable name="s1">
<xsl:call-template name="stringreplace">
  ...
</xsl:variable>
<xsl:variable name="s2">
<xsl:call-template name="stringreplace">
  <xsl:with-param name="text" select="$s1"/>
  <xsl:with-param name="replace" select="'&lt;&gt;'"/>
  <xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="stringreplace">
  <xsl:with-param name="text" select="$s2"/>
  <xsl:with-param name="replace" select="'&lt;/&gt;'"/>
  <xsl:with-param name="by" select="''"/>
</xsl:call-template>
On the other hand for simply removing certain characters the translate function suffices:
Code:
<xsl:variable name="s1">
<xsl:call-template name="stringreplace">
  ...
</xsl:variable>
<xsl:value-of select="translate($s1, '&lt;&gt;/', '')"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 15th, 2009, 11:56 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
You can store the result of calling a template in a variable and then you can use that variable when calling the template again, this time with different parameters:
*sigh* how come this seems so simple when you do it?
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old September 16th, 2009, 06:56 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Martin,

<xsl:variable name=""" & "s2" & """>
<xsl:call-template name=""" & "stringreplace" & """>
<xsl:with-param name=""" & "text" & """ select=""" & sAttValue & """/>
<xsl:with-param name=""" & "replace" & """ select=""" & "'</><>'" & """/>
<xsl:with-param name=""" & "by" & """ select=""" & "'|'" & """/>
</xsl:call-template>
</xsl:variable>

I have tried the above example but it is giving me the below error:

Exception: {0}System.Runtime.InteropServices.COMException (0x80004005):
Variable or parameter 's2' cannot be defined twice within the same template


How can i solve this problem

Thanks
-Nelly
 
Old September 16th, 2009, 07:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you will need to provide more details if you need help in finding out what causes that error.
Which XSLT processor exactly do you use?
And how does the stylesheet look that causes that error?
And please post the real stylesheet code as XML and not as VB concatenated strings.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 16th, 2009, 07:41 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thanks for your replies. I have found the problem and now its working.

Actually i was calling the template in a loop and same name variable was getting created more than once that was the problem.

Thanks again

-Nelly





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing multiple characters in a string philboparker BOOK: XSLT Programmer's Reference, 2nd Edition 0 May 20th, 2008 04:38 PM
Replacing tags within xml using xslt patelgaurav85 XSLT 0 June 19th, 2007 12:34 PM
replacing numbers in a string cole SQL Server 2000 4 April 2nd, 2007 04:24 PM
Replacing characters in a string semilemon C# 2005 2 June 16th, 2006 11:31 PM
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.