PDA

View Full Version : Replacing "\n" with "<br />" with XSL


mlse
07-20-2006, 03:50 PM
Hi,

What I'm trying to do is best described using some examples.

I want to convert this:


<myelem>Line 1\nLine 2\nLine 3</myelem>


To this:


<div>Line 1<br />Line 2<br />Line 3</div>


I've tried this:


<xsl:template match="myelem">
<div>
<xsl:value-of select="translate(., '\n', '<br />')" />
</div>
</xsl:template>


But I get the error:

The character '<' cannot be used in an attribute value. Error processing resource 'http://localhost/test.xml'...
<xsl:value-of select="translate(., '\n', '<br />')" />
------------------------------------------^


So perhaps the parser thinks I'm trying to make the substitution in one of the attributes of <myelem> ... which is not what I want!

I am pretty (actually very!) new to XSLT, so please bear with me while I'm still learning ...

Any help much appreciated!

Mike.

Alex Vincent
07-21-2006, 05:31 AM
<xsl:value-of select="translate(., '\n', '&lt;br /&gt;')" />


Try that.

mlse
07-21-2006, 12:39 PM
Hi, thanks for that. It doesn't throw up an error now, but it doesn't recognise the escape character on the '\n' and it's replacing only the 'b' in '<br />'.

Which means that:
nine naughty nuns nibbled nightly in nantucket

becomes:
bibe baughty bubs bibbled bightly ib babtucket

Lol!