23

Possible Duplicate:
Interpreting newlines with XSLT xsl:text?

How to convert newline into <br/> with XSLT?

I have this:

<text>
some text with 
new lines
</text>

I want to have this:

<p> some text with <br /> new lines </p>
a stone arachnid
  • 1,272
  • 1
  • 15
  • 27
liysd
  • 4,413
  • 13
  • 35
  • 38
  • Check this http://stackoverflow.com/questions/185101/interpreting-newlines-with-xslt-xsltext – PSK Jul 22 '10 at 14:15

1 Answers1

36

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="t">
  <p>
    <xsl:apply-templates/>
  </p>
 </xsl:template>

 <xsl:template match="text()" name="insertBreaks">
   <xsl:param name="pText" select="."/>

   <xsl:choose>
     <xsl:when test="not(contains($pText, '&#xA;'))">
       <xsl:copy-of select="$pText"/>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="substring-before($pText, '&#xA;')"/>
       <br />
       <xsl:call-template name="insertBreaks">
         <xsl:with-param name="pText" select=
           "substring-after($pText, '&#xA;')"/>
       </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>Line1
Line2
Line3
</t>

produces the wanted, correct result:

<p>Line1<br />Line2<br />Line3<br /></p>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • It doesn't work. I use jquery.xslt.js to make translation. Do you think it does matter? – liysd Jul 22 '10 at 15:13
  • 1
    @liysd: I have corrected my answer. Try it now. – Dimitre Novatchev Jul 22 '10 at 15:58
  • @liysd: Now, it works. @Dimitre: +1 also because I like this recursion pattern. –  Jul 22 '10 at 23:20
  • 1
    @Alejandro: I knew you'd like it, otherwise I'd recommend using FXSL's `str-map()` :) – Dimitre Novatchev Jul 23 '10 at 01:01
  • @Dimitre: Thanks! It started to work when I changed "text()" to "text" (I use tag instead of in your example). – liysd Jul 23 '10 at 17:38
  • it doesn't work with internet explorer. I jquery.xslt.js ends with error. – liysd Aug 09 '10 at 15:22
  • 2
    @liysd: Try never to write: "it doesn't work". Always explain what is "it" and what is "not working". So "it" was working before and now "it" is "not working"? Probably the change in behavior is only due that it started raining today? – Dimitre Novatchev Aug 09 '10 at 17:22
  • For some strange reason, it seems that Firefox will double the BR tag (that is it is outputting two successive BR tags instead of one) unless setting the method="html" attribute on the xsl:output tag. I am using Firefox version 8 and jQuery.transform. The behavior does not happen in IE9. – Florin Dumitrescu Nov 10 '11 at 16:17
  • @FlorinDumitrescu: THis needs to be clarified with the developers of Firefox -- possibly their bug. – Dimitre Novatchev Nov 10 '11 at 17:26
  • @Dimitre Novatchev, I agree, it is a FireFox problem, not a bug in your algorithm ( you have my +1 ;) ). I've put the comment here, since it might help others as well. – Florin Dumitrescu Nov 11 '11 at 10:38
  • Thank you for this, @DimitreNovatchev. Just one thing: for my project I converted this to a simple named template (for use with `xsl:call-template`) and found I had to change `xsl:copy-of` to `xsl:value-of` otherwise the end result was that pure text (anything without a `
    `) would be wrapped in an element with the name "pText", which led to invalid HTML5.
    – Bobulous Nov 01 '19 at 20:47
  • @Bobulous, Glad this answer was helpful. It was intended to provide a solution to this particular problem with the given XML document. Of course, if another XML document with a different schema is targeted, then this solution needs to be adjusted. One needs to specify the exact XPath expression of what must be selected or copied. – Dimitre Novatchev Nov 01 '19 at 20:54