How to do String Replace in XSL 1.0

To do String Replace in XSL 1.0 you can use the following snippet.

<xsl:template name="StringReplace">
  <xsl:param name="input" />
  <xsl:param name="search" />
  <xsl:param name="replace" />
  <xsl:choose>
    <xsl:when test="contains($input, $search)">
      <xsl:value-of select="substring-before($input,$search)" />
      <xsl:value-of select="$replace" />
      <xsl:call-template name="StringReplace">
        <xsl:with-param name="input"
        select="substring-after($input,$search)" />
        <xsl:with-param name="search" select="$search" />
        <xsl:with-param name="replace" select="$replace" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$input" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

The Template can be invoked as:

<xsl:variable name="newInput">
  <xsl:call-template name="StringReplace">
    <xsl:with-param name="input" select="$input" />
    <xsl:with-param name="search" select="string1" />
    <xsl:with-param name="replace" select="string2" />
  </xsl:call-template>
</xsl:variable>

Please Share us:
Please rate this snippet

3 thoughts on “How to do String Replace in XSL 1.0

Leave a Reply