Skip to content

Instantly share code, notes, and snippets.

@przmv
Created September 16, 2010 13:51
Show Gist options
  • Save przmv/582451 to your computer and use it in GitHub Desktop.
Save przmv/582451 to your computer and use it in GitHub Desktop.
String replace all with XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
<!-- template call -->
<xsl:variable name="result">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="Text to %REPLACE%" />
<xsl:with-param name="replace" select="'%REPLACE%'" />
<xsl:with-param name="by" select="'replace'" />
</xsl:call-template>
</xsl:variable>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment