Buhay Programmer
Dont byte more than you can chew

String.Replace() in XSLT

Tuesday, April 1, 2008 8:36 PM

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)




Feedback

# re: String.Replace() in XSLT

nice xslt! 7/31/2008 2:09 AM | al

# re: String.Replace() in XSLT

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
11/22/2008 1:36 AM | lavbox

# re: String.Replace() in XSLT

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. 1/9/2009 9:34 PM | Louisa Nicholson

# re: String.Replace() in XSLT

Thank you for your post!! 2/12/2009 4:41 AM | A. OLMOS

# re: String.Replace() in XSLT

Very good example for String replace... 2/16/2009 2:06 PM | Noor

# re: String.Replace() in XSLT

Man you saved my life!!! Thanx a lot!!! 2/22/2009 11:37 PM | Bohdan

# re: String.Replace() in XSLT

Thank you, very good... but what if I want to replace only the first occurrence? 3/11/2009 10:31 PM | Renato

# re: String.Replace() in XSLT

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>
3/14/2009 12:45 PM | mitcrellim

# re: String.Replace() in XSLT

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
3/20/2009 2:37 AM | ian

# re: String.Replace() in XSLT

thank you, thank you!! 3/22/2009 6:03 AM | dominic

# re: String.Replace() in XSLT

good
dfdf 3/26/2009 5:43 PM | dfd

# re: String.Replace() in XSLT

Just wanted to say : thank you :-) 4/17/2009 11:58 PM | laruiss

# re: String.Replace() in XSLT

How can we use above function to replace a quote (") by \" in given string. 6/4/2009 4:50 PM | nk

# re: String.Replace() in XSLT

how do we use above function to replace blank by <br> or next line? Pls let me know. 6/17/2009 2:58 PM | vn

# re: String.Replace() in XSLT

why is the code not available for copy/paste???? 7/15/2009 2:01 PM | fuck sake

# re: String.Replace() in XSLT

Bitmaps? for code? Hello? Are you sane? 7/17/2009 10:19 AM | Andrew

# re: String.Replace() in XSLT

Dude Code Looks good but Need to be able to copy pate...gr8 work and help though ...u are a life saver 7/22/2009 11:45 AM | ace

# re: String.Replace() in XSLT

thank you 7/23/2009 5:13 AM | sandy

# re: String.Replace() in XSLT

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> 8/13/2009 7:23 AM | Marky

# re: String.Replace() in XSLT

A simple code for a great solution. Many thanks man. 8/20/2009 10:54 AM | dust

# re: String.Replace() in XSLT

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>
11/13/2009 12:58 PM | granadaCoder

# re: String.Replace() in XSLT

This is what exactly I am looking for. Ton of thanks to you :-). 12/21/2009 10:15 AM | Sabarish

# re: String.Replace() in XSLT

Thanks for the solutions, it helps!
But is there a way for me to replace "\n" Or any regular expression? Thanks in advance. 12/23/2009 8:27 AM | meng

# re: String.Replace() in XSLT

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. 1/8/2010 6:06 AM | Muhammad Mosa

# re: String.Replace() in XSLT

helpfull template 3/11/2010 5:01 AM | srinivas

# re: String.Replace() in XSLT

Thanks so much for this simplified code. Nice one .... 4/19/2010 3:56 AM | Shahid Jamil

# re: String.Replace() in XSLT

It save my lift. Thanks! 6/13/2010 11:56 PM | burim

# re: String.Replace() in XSLT

@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;'" />


6/30/2010 5:47 AM | Sten Hougaard

# re: String.Replace() in XSLT

how can i replace double quotes(") with single quote(') 7/12/2010 7:51 AM | Abhishek Patel

# re: String.Replace() in XSLT

Thank you. It works fine.
9/1/2010 3:13 AM | Peter

# re: String.Replace() in XSLT

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... 10/7/2010 10:08 AM | Ravi Kumar Singh

# re: String.Replace() in XSLT

How about
translate()?
same syntax and same result! and nativ! 11/16/2010 10:25 AM | sierra

# re: String.Replace() in XSLT

@Abhishek Try &quot; and &apos;

@sierra I agree. translate() worked great for me. 11/16/2010 3:18 PM | Jason Green

# re: String.Replace() in XSLT

Thank you! :) 1/21/2011 2:59 AM | jes

# re: String.Replace() in XSLT

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 2/25/2011 2:41 AM | simplex

# re: String.Replace() in XSLT

How to call this function multiple time?

something equivallent in C# as string.Replace(...).Replace(..) 3/9/2011 10:17 AM | XSLT Seeker

# re: String.Replace() in XSLT

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... 3/23/2011 8:50 AM | Stew Ashton

# re: String.Replace() in XSLT

I need two double quotes replaced by an empty string. How do I do that in xsl? Thanks in advance. 3/24/2011 9:26 PM | dkrs0001

# re: String.Replace() in XSLT

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? 3/29/2011 10:51 PM | Sari

# re: String.Replace() in XSLT

hey this works so swiftly... cool 4/21/2011 6:03 AM | Neelam

# re: String.Replace() in XSLT

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. 4/21/2011 7:20 AM | natural sciatica treatment

# re: String.Replace() in XSLT

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! 5/26/2011 7:04 AM | dll file extension

# re: String.Replace() in XSLT

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] 8/24/2011 9:55 AM | Kim

# re: String.Replace() in XSLT

Thank you. This works nicely. 9/23/2011 10:24 AM | Olde Fortran 77

# re: String.Replace() in XSLT

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). 10/3/2011 3:34 AM | Rüdiger

# re: String.Replace() in XSLT

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. 10/23/2011 1:11 AM | sandeep

# re: String.Replace() in XSLT

awesome. thanks! 10/25/2011 11:43 AM | wh

# re: String.Replace() in XSLT

Very helpful, saved me the trouble of having to write my own. Thanks for sharing! 12/17/2011 10:44 AM | MBM

# re: String.Replace() in XSLT

Thank you sir 2/7/2012 1:36 PM | Moussa

# re: String.Replace() in XSLT

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. 2/9/2012 12:58 PM | Ahmun

# re: String.Replace() in XSLT

good one. 4/12/2012 2:56 AM | Gaurav Balyan

# re: String.Replace() in XSLT

Very good useful material..helped me in critical circumstance in my project .... Thanks a lot 5/28/2012 8:25 AM | Antony

# re: String.Replace() in XSLT

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 6/12/2012 3:18 AM | Yeshwant Kakad

# re: String.Replace() in XSLT

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. 6/22/2012 9:20 AM | Web Design Macclesfield

# re: String.Replace() in XSLT

Excellent; thanks. 9/2/2012 10:50 AM | David McCormick

# re: String.Replace() in XSLT

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] 3/27/2013 12:55 AM | Olivier

# re: String.Replace() in XSLT

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 8/2/2013 10:31 AM | Michael Barber

# re: String.Replace() in XSLT

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 11/27/2014 12:58 AM | bobi

Post a comment