Created
May 10, 2012 14:42
-
-
Save xpathr/2653565 to your computer and use it in GitHub Desktop.
String Replace by phoque
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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