Geeks With Blogs
Buhay Programmer Dont byte more than you can chew

Here's a sample template that lets you have the String.Replace() functionality in XSLT 1.0.   The template "string-replace-all" takes 3 parameters and recursively processes the input text string.

  •      text         : main string
  •      replace : the string fragment to be replaced
  •     by           :  the replacement string
     
 <xsl:template name="string-replace-all">
    <
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="string-replace-all">
          <
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's how it is called: 

  <
xsl:variable name="myVar">
    <
xsl:call-template name="string-replace-all">
      <
xsl:with-param name="text" select="'This is a sample text : {ReplaceMe} and {ReplaceMe}'" />
      <
xsl:with-param name="replace" select="'{ReplaceMe}'" />
      <
xsl:with-param name="by" select="'String.Replace() in XSLT'" />
    </
xsl:call-template>
  </
xsl:variable>

(Edit : Thanks to Marky and granadaCoder for typing out the xslt code in the comments.)

The resulting value of $myVar after {ReplaceMe} is replaced is "This is a sample text : String.Replace() in XSLT and String.Replace() in XSLT"

For those who are not familiar with XSLT syntax and here's the C# equivalent.  An excellent material for the thedailywtf! :)

 (Note: I'm not so sure, but I think in XSL 2.0 there is already a built-in replace function on strings)

Posted on Tuesday, April 1, 2008 8:36 PM | Back to top


Comments on this post: String.Replace() in XSLT

# re: String.Replace() in XSLT
Requesting Gravatar...
nice xslt!
Left by al on Jul 31, 2008 2:09 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Nice post.
I have written a string replace template too.
Check it out
http://lavbox.blogspot.com/2008/05/xsl-template-to-split-to-string.html
Left by lavbox on Nov 22, 2008 1:36 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thanks a bunch for posting this - a software I'm using doesn't have the XSLT2 engine and I had to implement this functionality quickly in version 1 compliant code.
Left by Louisa Nicholson on Jan 09, 2009 9:34 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you for your post!!
Left by A. OLMOS on Feb 12, 2009 4:41 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Very good example for String replace...
Left by Noor on Feb 16, 2009 2:06 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Man you saved my life!!! Thanx a lot!!!
Left by Bohdan on Feb 22, 2009 11:37 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you, very good... but what if I want to replace only the first occurrence?
Left by Renato on Mar 11, 2009 10:31 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
To only replace the first occurrence, simply take out the recursion call. Something like this:

<xsl:template name="string-replace-all">
<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:with-param name="text" select="substring-after($text,$replace)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Left by mitcrellim on Mar 14, 2009 12:45 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thanks - that almost made life easy for me until I realized that all i needed for my particular case was the translate() function! So if anyone else sees this post and uses xslt too infrequently to remember how to do really basic things - translate will replace any instances of EACH CHARACTER with another, so it's good for single char stuff like stripping slashes!
http://www.w3schools.com/Xpath/xpath_functions.asp
Left by ian on Mar 20, 2009 2:37 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
thank you, thank you!!
Left by dominic on Mar 22, 2009 6:03 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
good
dfdf
Left by dfd on Mar 26, 2009 5:43 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Just wanted to say : thank you :-)
Left by laruiss on Apr 17, 2009 11:58 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
How can we use above function to replace a quote (") by \" in given string.
Left by nk on Jun 04, 2009 4:50 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
how do we use above function to replace blank by <br> or next line? Pls let me know.
Left by vn on Jun 17, 2009 2:58 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
why is the code not available for copy/paste????
Left by fuck sake on Jul 15, 2009 2:01 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Bitmaps? for code? Hello? Are you sane?
Left by Andrew on Jul 17, 2009 10:19 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Dude Code Looks good but Need to be able to copy pate...gr8 work and help though ...u are a life saver
Left by ace on Jul 22, 2009 11:45 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
thank you
Left by sandy on Jul 23, 2009 5:13 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
And here the whole stuff for copy-and-paste:

<xsl:template name="string-replace-all">
<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="string-replace-all">
<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>
Left by Marky on Aug 13, 2009 7:23 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
A simple code for a great solution. Many thanks man.
Left by dust on Aug 20, 2009 10:54 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Where's the code download?
Here is the sample test typed out. (Thanks to the person who typed out the implementation).

Thanks for the post.


<xsl:variable name="myVar">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="'This is a sample text : {ReplaceMe} and {ReplaceMe}'" />
<xsl:with-param name="replace" select="'{ReplaceMe}'" />
<xsl:with-param name="by" select="'String.Replace() in XSLT'" />
</xsl:call-template>
</xsl:variable>
Left by granadaCoder on Nov 13, 2009 12:58 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
This is what exactly I am looking for. Ton of thanks to you :-).
Left by Sabarish on Dec 21, 2009 10:15 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thanks for the solutions, it helps!
But is there a way for me to replace "\n" Or any regular expression? Thanks in advance.
Left by meng on Dec 23, 2009 8:27 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Because I tried it and it worked with me from first time, with now headache I though to comment and thank you about it.
Clean good snippet.
Left by Muhammad Mosa on Jan 08, 2010 6:06 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
helpfull template
Left by srinivas on Mar 11, 2010 5:01 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thanks so much for this simplified code. Nice one ....
Left by Shahid Jamil on Apr 19, 2010 3:56 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
It save my lift. Thanks!
Left by burim on Jun 13, 2010 11:56 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
@meng:
".But is there a way for me to replace "\n"."
Yes you can replace the two chars 13 and 13 to what you may wish to replace them with. That is you can set the parameter as such:
<xsl:with-param name="replace" select="'&#13;&#10;'" />


Left by Sten Hougaard on Jun 30, 2010 5:47 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
how can i replace double quotes(") with single quote(')
Left by Abhishek Patel on Jul 12, 2010 7:51 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you. It works fine.
Left by Peter on Sep 01, 2010 3:13 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Its a great post, it takes a good amount of hardwork and dedication to discover these piece of codes.
It is highly appreciated and thanks a ton to you...
Left by Ravi Kumar Singh on Oct 07, 2010 10:08 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
How about
translate()?
same syntax and same result! and nativ!
Left by sierra on Nov 16, 2010 10:25 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
@Abhishek Try &quot; and &apos;

@sierra I agree. translate() worked great for me.
Left by Jason Green on Nov 16, 2010 3:18 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you! :)
Left by jes on Jan 21, 2011 2:59 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Here is how to replace multiple fragments in a string. This a kind of "translate":

<xsl:template name="string-translate">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:param name="delimiter" select="';'"/>
<xsl:choose>
<xsl:when test="string-length($replace) > 0 and string-length($by) > 0">
<xsl:variable name="replace-delimited">
<xsl:choose>
<xsl:when test="contains($replace, $delimiter)">
<xsl:value-of select="$replace" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($replace, $delimiter)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="by-delimited">
<xsl:choose>
<xsl:when test="contains($by, $delimiter)">
<xsl:value-of select="$by" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($by, $delimiter)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="temp" >
<xsl:call-template name="string-replace-all" >
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="replace" select="substring-before($replace-delimited, $delimiter)" />
<xsl:with-param name="by" select="substring-before($by-delimited, $delimiter)" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="string-translate" >
<xsl:with-param name="text" select="$temp" />
<xsl:with-param name="replace" select="substring-after($replace, $delimiter)" />
<xsl:with-param name="by" select="substring-after($by, $delimiter)" />
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

The template is called as follows:

<xsl:call-template name="string-translate" >
<xsl:with-param name="text" select="http%3A%2F%2Fgoogle%2Ecom" />
<xsl:with-param name="replace" select="'%2F %3A %2E'" />
<xsl:with-param name="by" select="'/ : .'" />
<xsl:with-param name="delimiter" select="' '"/>
</xsl:call-template>

The result of the a.m. call will be: http://google.com
Left by simplex on Feb 25, 2011 2:41 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
How to call this function multiple time?

something equivallent in C# as string.Replace(...).Replace(..)
Left by XSLT Seeker on Mar 09, 2011 10:17 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
I am writing a generic XML to CSV xslt file, and I needed to double all quotes in the character fields since I am putting quotes around them.

I was able to do this in no time with your code. Many, many thanks...
Left by Stew Ashton on Mar 23, 2011 8:50 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
I need two double quotes replaced by an empty string. How do I do that in xsl? Thanks in advance.
Left by dkrs0001 on Mar 24, 2011 9:26 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
I tried with the string-replace-all function. It is working for most of the cases. But in one case alone I am getting the below exception,

Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?

Line: 176
Error: The XSL processor stack has overflowed - probable cause is infinite template recursion.


---------------------------
Yes No
---------------------------
Can you give me some idea how it happened?
Left by Sari on Mar 29, 2011 10:51 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
hey this works so swiftly... cool
Left by Neelam on Apr 21, 2011 6:03 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
I think very good article. Nice design.Your site always offer some really interesting information. Thank you for sharing it with us.The information provide by you is very useful for me.thanks for sharing and hope you will share again.I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
Left by natural sciatica treatment on Apr 21, 2011 7:20 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
What you posted is very good. I have been looking for a String.Replace functionality in XSLT 1.0, but I would have never hoped to find something that would allow me to take three parameters and recursively process the input text string. Thanks for uploading this!
Left by dll file extension on May 26, 2011 7:04 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Improved version for multiple string replace:

[code]<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="by"/>
<xsl:param name="separator"/>
<xsl:choose>
<xsl:when test="$separator and contains($replace, $separator)">
<xsl:variable name="text2">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="substring-before($replace, $separator)"/>
<xsl:with-param name="by" select="substring-before($by, $separator)"/>
<xsl:with-param name="separator" select="$separator"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$text2"/>
<xsl:with-param name="replace" select="substring-after($replace, $separator)"/>
<xsl:with-param name="by" select="substring-after($by, $separator)"/>
<xsl:with-param name="separator" select="$separator"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<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="string-replace-all">
<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:otherwise>
</xsl:choose>
</xsl:template>
[/code]
Left by Kim on Aug 24, 2011 9:55 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you. This works nicely.
Left by Olde Fortran 77 on Sep 23, 2011 10:24 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thanks for posting this! S I didn't have to reinvent the wheel. I recently incorporated the template in my JSON builder (http://ruediger-plantiko.blogspot.com/2011/09/ein-json-builder-in-abap.html).
Left by Rüdiger on Oct 03, 2011 3:34 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Dear's

Assuming that Parameters are not passed from param & will be declard within the XSLT script.Can anybody Pls help me to get the exact code.
Left by sandeep on Oct 23, 2011 1:11 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
awesome. thanks!
Left by wh on Oct 25, 2011 11:43 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Very helpful, saved me the trouble of having to write my own. Thanks for sharing!
Left by MBM on Dec 17, 2011 10:44 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Thank you sir
Left by Moussa on Feb 07, 2012 1:36 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
Any hints on how to replace the ampersand?

For setting up the replace parameter I tried:
<xsl:with-param name="replace" select="'&'" />
<xsl:with-param name="by" select="'%26'" />
And that threw an error.

I then tried:
<xsl:with-param name="replace" select="'&amp;'" />
<xsl:with-param name="by" select="'%26'" />
And no error, however, the return variable was an empty string.

I ended up using some crazy combination of FIND and REPLACE function use in a calculated column.

What I'm trying to accomplish is present a list of contractors in a custom list where clicking their name will direct the user to another page with the contractor's name as a querystring value. Something like "Father & Son Contractor" would throw off my querystring since the ampersand is the delimiter for multiple querystring params.
Left by Ahmun on Feb 09, 2012 12:58 PM

# re: String.Replace() in XSLT
Requesting Gravatar...
good one.
Left by Gaurav Balyan on Apr 12, 2012 2:56 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Very good useful material..helped me in critical circumstance in my project .... Thanks a lot
Left by Antony on May 28, 2012 8:25 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Hi, I tried to use above function for replacing &lt with < but it failed. I copied same function but it does not accept '<' or "<". See the error:
The content of elements must consist of well-formed character data or markup.

Can anybody please tell me how to replace &lt with < sign? thanks. Yeshwant
Left by Yeshwant Kakad on Jun 12, 2012 3:18 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Does anybody know if there is there an "ends-with" type function.. whereby I can detect if the string ends with a dot, and if so I can then trim it off?

I am trying to have some text followed by ... (3 dots)

but if the text already has a dot, then I end up with 4 dots (....)

A niggle I know, but would be good if I could fix it.
Left by Web Design Macclesfield on Jun 22, 2012 9:20 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Excellent; thanks.
Left by David McCormick on Sep 02, 2012 10:50 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
For those using XSLT with PHP, here's a version which use <xsl:processing-instruction /> :

[code]<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:processing-instruction name="php">
echo str_replace('<xsl:value-of select="$replace" />', '<xsl:value-of select="$by" />', '<xsl:value-of select="$text" />');
</xsl:processing-instruction>
</xsl:template>[/code]
Left by Olivier on Mar 27, 2013 12:55 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
I have been using this for awhile but how can you do this for two possible replaces in the same string. So replace string0 with string1 -or- replace string2 with string3
Left by Michael Barber on Aug 02, 2013 10:31 AM

# re: String.Replace() in XSLT
Requesting Gravatar...
Can anyone help me with replacing special characters. Whenever i try to replace singleQuote with \' it messes up the preview and something is not working.

I tried:
1)replace:"'''"
by:"'\'"

2)replace:"'\''"
by:"'\\\''"

3)replace"'&apos;'"
by:"'\&apos;'"

please help :D
Left by bobi on Nov 27, 2014 12:58 AM

Your comment:
 (will show your gravatar)


Copyright © Erik Araojo | Powered by: GeeksWithBlogs.net | Join free