Re: Convert newlines to <BR/>

Subject: Re: Convert newlines to <BR/>
From: "Steve Muench" <smuench@xxxxxxxxxxxxx>
Date: Tue, 28 Dec 1999 23:27:17 -0800
This solution works for me:

Given the input document:

<doc>
  <p>This is some text.</p>
  <programlisting><![CDATA[This is a paragraph
  with some newlines
  does it work?]]></programlisting>
</doc>

The stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="/">
<html>
  <head/>
  <body>
  <xsl:apply-templates/>
  </body>
</html>
</xsl:template>

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

<xsl:template match="programlisting">
  <span style="font-family:monospace">
  <xsl:call-template name="br-replace">
    <xsl:with-param name="word" select="."/>
  </xsl:call-template>
  </span>
</xsl:template>

<xsl:template name="br-replace">
  <xsl:param name="word"/>
   <!-- </xsl:text> on next line on purpose to get newline -->
  <xsl:variable name="cr"><xsl:text>
</xsl:text></xsl:variable>
  <xsl:choose>
  <xsl:when test="contains($word,$cr)">
      <xsl:value-of select="substring-before($word,$cr)"/>
      <br/>
      <xsl:call-template name="br-replace">
        <xsl:with-param name="word" select="substring-after($word,$cr)"/>
      </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$word"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

</xsl:stylesheet>


Produces:

<html>
<head>
</head>
<body>
  <p>This is some text.</p>
  <span style="font-family:monospace">This is a paragraph<br>  with some
newlines<br>  does it work?</span>
</body>
</html>

Which seems to be like what you want...

_________________________________________________________
Steve Muench, Consulting Product Manager & XML Evangelist
Business Components for Java Development Team
http://technet.oracle.com/tech/java
http://technet.oracle.com/tech/xml
----- Original Message -----
From: "Jon Smirl" <jonsmirl@xxxxxxxxxxxx>
To: "XSLList" <xsl-list@xxxxxxxxxxxxxxxx>
Sent: Tuesday, December 28, 1999 10:10 PM
Subject: Convert newlines to <BR/>


| I have text that contains newlines and I want to replace these with <BR/>.
I
| can't use <PRE> for the output because I need word-wrap to work. Any ideas
| on how to do the conversion? translate(a, b, c) only does single character
| replacements.
|
| Using XT.
|
| Jon Smirl
| jonsmirl@xxxxxxxxxxxx
|
|
|
|  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
|


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread