string.subst

string.subst — Substitute one text string for another in a string

Description

The string.subst template replaces all occurances of target in string with replacement and returns the result.

<xsl:template name="string.subst">
  <xsl:param name="string"></xsl:param>
  <xsl:param name="target"></xsl:param>
  <xsl:param name="replacement"></xsl:param>

  <xsl:choose>
    <xsl:when test="contains($string, $target)">
      <xsl:variable name="rest">
        <xsl:call-template name="string.subst">
          <xsl:with-param name="string" select="substring-after($string, $target)"></xsl:with-param>
          <xsl:with-param name="target" select="$target"></xsl:with-param>
          <xsl:with-param name="replacement" select="$replacement"></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="concat(substring-before($string, $target),                                    $replacement,                                    $rest)"></xsl:value-of>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"></xsl:value-of>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>