String.Replace() in XSLT

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 

 

Here's how it is called: 

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)

Print | posted on Tuesday, April 01, 2008 8:36 PM

Feedback

# re: String.Replace() in XSLT

left by al at 7/31/2008 2:09 AM Gravatar
nice xslt!

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

left by A. OLMOS at 2/12/2009 4:41 AM Gravatar
Thank you for your post!!

# re: String.Replace() in XSLT

left by Noor at 2/16/2009 2:06 PM Gravatar
Very good example for String replace...

# re: String.Replace() in XSLT

left by Bohdan at 2/22/2009 11:37 PM Gravatar
Man you saved my life!!! Thanx a lot!!!

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

left by dominic at 3/22/2009 6:03 AM Gravatar
thank you, thank you!!

# re: String.Replace() in XSLT

left by dfd at 3/26/2009 5:43 PM Gravatar
good
dfdf

# re: String.Replace() in XSLT

left by laruiss at 4/17/2009 11:58 PM Gravatar
Just wanted to say : thank you :-)

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

left by Andrew at 7/17/2009 10:19 AM Gravatar
Bitmaps? for code? Hello? Are you sane?

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

left by sandy at 7/23/2009 5:13 AM Gravatar
thank you

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

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

# re: String.Replace() in XSLT

left by Thank You at 10/18/2009 11:47 PM Gravatar
Thank You! Thank You! Thank You!
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: