Search/Replace Strings template

Last change 25/04/2014

Search Replace Functionality with XSL 1.0

It happens more often than you think, that you have to do a real search/replace within a text node. this template does exactly that.

A typical usage would be:

<xsl:call-template name="replaceString">
   <xsl:with-param name="value" select="text()"/>
   <xsl:with-param name="s" select="'test'"/>
   <xsl:with-param name="r" select="'tester'"/>

    Replaces a string with another
    s: the search string
    r: the replace string
    value: the string to search in
<xsl:template name="replaceString">
    <xsl:param name="value"/>
    <xsl:param name="s"/>
    <xsl:param name="r"/>

        <xsl:when test="$value = ''"> </xsl:when>
        <xsl:when test="$s = ''">
            <xsl:value-of select="$value"/>
        <xsl:when test="contains($value, $s)">

            <xsl:value-of select="substring-before($value, $s)"/>
            <xsl:value-of select="$r"/>
            <xsl:call-template name="replaceString">
                <xsl:with-param name="value" select="substring-after($value, $s)"/>
                <xsl:with-param name="s" select="$s"/>
                <xsl:with-param name="r" select="$r"/>
            <xsl:value-of select="$value"/>

Site created with Corinis CCM