Skip to content

Instantly share code, notes, and snippets.

@xpathr
Created May 10, 2012 14:42
Show Gist options
  • Select an option

  • Save xpathr/2653565 to your computer and use it in GitHub Desktop.

Select an option

Save xpathr/2653565 to your computer and use it in GitHub Desktop.
String Replace by phoque
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Example call
<xsl:call-template name="string-replace">
<xsl:with-param name="haystack" select="item" />
<xsl:with-param name="search" select="'ab'" />
<xsl:with-param name="replace" select="'zz'" />
</xsl:call-template>
-->
<xsl:template name="string-replace">
<xsl:param name="haystack" />
<xsl:param name="search" />
<xsl:param name="replace" select="''" />
<xsl:choose>
<xsl:when test="contains($haystack, $search)">
<xsl:value-of select="substring-before($haystack, $search)" />
<xsl:value-of select="$replace" />
<xsl:call-template name="string-replace">
<xsl:with-param name="haystack" select="substring-after($haystack, $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="$haystack" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment