Monday, July 2, 2007

XSL: Replace string with element

Replace encoded Carriage Return "
" with br element

<xsl:template match="sectionBody">
<xsl:apply-templates select="text()"></xsl:apply-templates>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="break">
</xsl:call-template>

<xsl:template name="break">
<xsl:param name="text" select=".">
<xsl:choose>
<xsl:when test="contains($text, '&#xD;')">
<xsl:value-of select="substring-before($text, '&#xD;')">
<br/>

<xsl:call-template name="break">
<xsl:with-param name="text" select="substring-after($text, '&#xD;')">
</xsl:with-param>
</xsl:call-template>
<xsl:otherwise>
<xsl:value-of select="$text">
</xsl:value-of>
</xsl:otherwise>
</xsl:value-of>
</xsl:when></xsl:choose></xsl:param></xsl:template></xsl:template>

for more XSL replace hints read http://www.dpawson.co.uk/xsl/sect2/replace.html

1 comment:

Tigerlilly said...

Many thanks - this was really helpful! This is stuff I have to remember too. :)