Created
September 15, 2013 16:56
-
-
Save ijy/6572481 to your computer and use it in GitHub Desktop.
Alternative search and replace template for XSLT 1.0.
This file contains 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
<!-- | |
ALTERNATIVE SEARCH & REPLACE | |
string: The text to be evaluated | |
replace: The character or string to look for in the above string | |
with: What to replace it with | |
Slightly more long winded approach if that's how you prefer to roll. | |
--> | |
<xsl:template name="string-replace"> | |
<xsl:param name="string" /> | |
<xsl:param name="replace" /> | |
<xsl:param name="with" /> | |
<xsl:choose> | |
<xsl:when test="contains($string, $replace)"> | |
<xsl:value-of select="substring-before($string, $replace)" /> | |
<xsl:value-of select="$with" /> | |
<xsl:call-template name="string-replace"> | |
<xsl:with-param name="string" select="substring-after($string,$replace)" /> | |
<xsl:with-param name="replace" select="$replace" /> | |
<xsl:with-param name="with" select="$with" /> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$string" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
<!-- Example template call --> | |
<xsl:variable name="string-mod"> | |
<xsl:call-template name="string-replace"> | |
<xsl:with-param name="string" select="'This is a sample text : {replace} and {replace}'" /> | |
<xsl:with-param name="replace" select="'{replace}'" /> | |
<xsl:with-param name="with" select="'{I've been replaced}" /> | |
</xsl:call-template> | |
</xsl:variable> | |
<!-- Output --> | |
<xsl:value-of select="string-mod" /> | |
<!-- Result: "This is a sample text : {I've been replaced} and {I've been replaced}" --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The with param tag is missing a [']. It should be: <xsl:with-param name="with" select="'{I've been replaced}'" />