Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created October 8, 2012 13:40
Show Gist options
  • Save netsi1964/3852588 to your computer and use it in GitHub Desktop.
Save netsi1964/3852588 to your computer and use it in GitHub Desktop.
XSLT replace
<xsl:variable name="amp">
<xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
</xsl:variable>
<xsl:variable name="crlf" select="'&#13;&#10;'" />
<xsl:variable name="br">
<xsl:text disable-output-escaping="yes"><![CDATA[<br />]]></xsl:text>
</xsl:variable>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="'Your original'" />
<xsl:with-param name="replace" select="$br" />
<xsl:with-param name="by" select="$crlf" />
</xsl:call-template>
<xsl:call-template name="string-replace-all_outputEscapingDisabled">
<xsl:with-param name="text" select="'Your original'" />
<xsl:with-param name="replace" select="$br" />
<xsl:with-param name="by" select="$crlf" />
</xsl:call-template>
<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>
<xsl:template name="string-replace-all_outputEscapingDisabled">
<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)" disable-output-escaping="yes" />
<xsl:value-of select="$by" disable-output-escaping="yes" />
<xsl:call-template name="string-replace-all_outputEscapingDisabled">
<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" disable-output-escaping="yes" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment