RE: [xsl] Translating the new lines to line breaks with HTML transformation

Subject: RE: [xsl] Translating the new lines to line breaks with HTML transformation
From: "Soyer, Muhammed A." <MSoyer@xxxxxxxxxx>
Date: Fri, 9 Dec 2011 15:58:21 +0000
It worked great.. Thanks a lot..


-----Original Message-----
From: G. Ken Holman [mailto:gkholman@xxxxxxxxxxxxxxxxxxxx]
Sent: Friday, December 09, 2011 10:51 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx; xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Translating the new lines to line breaks with HTML
transformation

At 2011-12-09 15:02 +0000, Soyer, Muhammed A. wrote:
>   I am using XSLT 1.0 and using is not an option at this time.

I'm assuming you mean not using XSLT 2.0.

>I have XML data as below and I need to transform to HTML. I want to
>insert line breaks for the DATES field for the new lines while
>transforming. I tried to use translate but it only replaces one
>character with another character.
>
>I am very new to XSL, any suggestion would be great..

You need a recursive call that walks through the text node looking
for newlines at the end of each line.  XML processing normalizes
end-of-line sequences to be a newline on all platforms.  The
recursive call keeps calling itself until all the newlines are processed.

I hope the example below helps.  I would note that very rarely will
you ever need to address text() nodes directly (there is a recent
thread on this), and when using select using "./*" is no different
than using "*".

. . . . . . . Ken

T:\ftemp>type soyer.xml
<?xml version="1.0"?>
<ROWSET>
  <ROW>
   <SEASON_CODE>HIGH</SEASON_CODE>
   <SEASON_DESC>High Season</SEASON_DESC>
   <DATES>Thu 2005-09-01 - Wed 2005-11-30
Fri 2006-09-01 - Sun 2006-12-03
Tue 2011-11-01 - Sat 2011-12-17</DATES>
  </ROW>
</ROWSET>

T:\ftemp>xslt soyer.xml soyer.xsl
<html>
    <body>
       <table border="1">
          <tr bgcolor="cyan">
             <th>SEASON_CODE</th>
             <th>SEASON_DESC</th>
             <th>DATES</th>
          </tr>
          <tr>
             <td>HIGH</td>
             <td>High Season</td>
             <td>Thu 2005-09-01 - Wed 2005-11-30<br>Fri 2006-09-01 -
Sun 2006-12-
03<br>Tue 2011-11-01 - Sat 2011-12-17
             </td>
          </tr>
       </table>
    </body>
</html>
T:\ftemp>type soyer.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="html"/>
<xsl:template match="/">
<html>
  <body>
   <table border="1">
     <tr bgcolor="cyan">
      <xsl:for-each select="/ROWSET/ROW[1]/*">
       <th><xsl:value-of select="name()"/></th>
      </xsl:for-each>
     </tr>
     <xsl:for-each select="/ROWSET/*">
      <tr>
       <xsl:for-each select="./*">
        <td>
          <xsl:call-template name="split-text">
            <xsl:with-param name="text" select="."/>
          </xsl:call-template>
        </td>
       </xsl:for-each>
      </tr>
     </xsl:for-each>
   </table>
   </body>
</html>
</xsl:template>

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

</xsl:stylesheet>
T:\ftemp>

--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/t37DVX
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

Current Thread