Click to See Complete Forum and Search --> : Search and Replace in XSLT


tomhartland
02-05-2004, 05:13 AM
I'm posting this as a sort-of cross thread of http://forums.webdeveloper.com/showthread.php?s=&threadid=27004, in a way to try and tackle the problem from a different angle.

I am trying to achieve "search and replace" functionality within my XSLT, where any instances of carriage return and/or new lines are replaced with <br> tags in the resultant HTML file.

So if I was to have the following XML...<TEXTTAG>This is before,
and this is after the carriage return.</TEXTTAG>...the output would be...
This is before,<br>and this is after the carriage return.If anybody could give me any advice I'd really appreciate it
Cheers,
Tom :)

crh3675
02-08-2004, 09:17 PM
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" version="1.0" standalone="yes" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>


<xsl:template match="/">
<xsl:call-template name="break">
<xsl:with-param name="text">this is a
test</xsl:with-param>
</xsl:call-template>
</xsl:template>

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

</xsl:stylesheet>

tomhartland
02-09-2004, 04:43 AM
You, my friend, are a COMPLETE STAR...!
Thank you :D

crh3675
02-09-2004, 09:42 AM
It's unfortunate that it requires so much code to do something so simple. And I though XML was the code of the future