ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Search and Replace
Submitter: Paul Prescod (other recipes)
Last Updated: 2001/06/27
Version no: 1.0
Category:

 

5 stars 3 vote(s)


Approved

Description:

XSLT has no first-class function to search-and-replace. Instead this
recipe implements it as a template.

Source: Text Source

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

 <!-- reusable replace-string function -->
 <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>

    <xsl:choose>
      <xsl:when test="contains($text, $from)">

	<xsl:variable name="before" select="substring-before($text, $from)"/>
	<xsl:variable name="after" select="substring-after($text, $from)"/>
	<xsl:variable name="prefix" select="concat($before, $to)"/>

	<xsl:value-of select="$before"/>
	<xsl:value-of select="$to"/>
        <xsl:call-template name="replace-string">
	  <xsl:with-param name="text" select="$after"/>
	  <xsl:with-param name="from" select="$from"/>
	  <xsl:with-param name="to" select="$to"/>
	</xsl:call-template>
      </xsl:when> 
      <xsl:otherwise>
        <xsl:value-of select="$text"/>  
      </xsl:otherwise>
    </xsl:choose>            
 </xsl:template>

 <!-- test the function -->
 <xsl:template match="/">
     <xsl:call-template name="replace-string">
        <xsl:with-param name="text" 
             select="'Mary had a little lamb, little lamb, little lamb.'"/>
        <xsl:with-param name="from" select="'little lamb'"/>
        <xsl:with-param name="to" select="'little steak'"/>
    </xsl:call-template>
 </xsl:template>
</xsl:stylesheet>

The license for this recipe is available here.

Discussion:

There are various versions of this function floating around. This one
is based upon Steve Ball's XSLTSL. You can find out more about that
package here: http://xsltsl.sourceforge.net/



Add comment

Number of comments: 10

How do I replace a single quote or apostrophe ( ' ) with either "&apos;" or "&#39;"?, Michael Sobczak, 2002/04/11

Add comment

Re: apostrophe escaping, Not specified Not specified, 2005/06/20

    <xsl:call-template name="replace-string">
        <xsl:with-param name="text">This sentence isn't apostrophe-free<xsl:with> 
        <xsl:with-param name="from">'</xsl:with-param>
        <xsl:with-param name="to" select="'&amp;apos;'"/>
    </xsl:call-template>

Add comment

XMLStick, vasa cheh, 2002/06/03
Wonderful sample! Very usefullll!!!! Thanks!
Add comment

There is a search and replace function in XSLT., Pontus Östlund, 2005/05/29
This is how it can be done:

<xsl:variable name="var" select="'this:is:a:sentence'"/>
<xsl:value-of select="translate($var, ':', ' ')"/>

Add comment

Re: translate(), Not specified Not specified, 2005/06/20
Yes, but translate() only replaces one-character strings with one-character strings. There is no way to replace 'little lamb' with 'little steak'.
Add comment

$prefix, Not specified Not specified, 2005/06/20
Thanks for a useful sample. Question: the $prefix variable appears never to be used. What's it there for?
Add comment

Xsl, nimesh maheshwari, 2005/09/26
Hi I tried the search and replace function but it creates a new table for my code instead of putting them in the same column from which i am picking up the data, any idea. thanks nimesh
Add comment

Replacing http://www.abc.com with &lt;a href='http://www.abc.com '&gt;http://www.abc.com </a>, manish goyal, 2006/04/21
I have a node called <description> some desc

</description>. The node exactly look like this:
<forum>
 <description>
Some text will come over here with a link http://www.abc.com after this also there can be text and then a link https://www.xyz.com and again some text.
 <description>
What I want is while XSL is transforming the the above node...it should transform in such a way that http://www.abc.com behaves like a link...so when anybody clicks on that it open a browser window. In a summary I would like to append <a href> tag to all the occurence of http:// or https://.
Please help me out.
Add comment

Thanks, Francesco Mannella, 2006/08/25
Your code is simply great!!! I'm quite new of XSLT and it saved me from an headache. Thank's
Add comment

Two replacements, sasa sasa, 2007/10/10
And what if I need to make two replacements on the same string? What should I do? Thanks.
Add comment



Highest rated recipes:

1. Search and Replace

2. Generating a newline

3. Internationalization ...

4. Result Pagination with ...

5. Restricting processing ...

6. Fetching information ...

7. Getting text children of ...

8. Creating empty elements




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.