tags:

views:

162

answers:

4

Hi,

I'm trying to preserve row breaks in an xml file when transforming it to html, but I cant find a way that works.

<meta>
 <name>Message</name>
 <value>Hi!

 I need info!

 Mr Test</value>
</meta>

And I use this xsl:

  <xsl:if test="name='Message'">
  <tr>
    <th align="left" colspan="2">Message:</th>
  </tr>
  <tr>
    <td colspan="2"><xsl:value-of select="value"/></td>
  </tr>
  </xsl:if>

But the new line (cr/lf) characters dissapear, and everything becomes one single line in html. Is is possible to match the cr/lf and replace them with html "<_br >", or any other method?

+1  A: 

You need to use <xsl:preserve-space> for this.

See this w3cSchools article for details.

Oded
This doesn't help, although the transform output may contain more whitespace than it did before HTML would still ignore the LFs. The problem isn't with the output white space its with how HTML renders content.
AnthonyWJones
Thanks for your answer, though that does not work since I have not used the strip-space first, and preserve is default.
kaze
A: 

I think you'd need something like

<meta>
    <name>Message</name>
    <value>Hi!
    &lt;BR&gt;I need info!
    &lt;BR&gt;Mr Test</value>

</meta>

which would outpurt something like

<tr> <td colspan="2"> Hi! <BR>I need info! <BR>Mr Test </td> </tr>

Raj
+2  A: 

Add the following template to your XSL:-

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

Now replace where select the value with a call to this template:-

<td colspan="2">
 <xsl:call-template name="LFsToBRs">
  <xsl:with-param name="input" select="value"/>
 </xsl:call-template>
</td>
AnthonyWJones
Excellent, just what I wanted, thanks!
kaze
A: 

Greetings, if you already have LF or CR in the source xml (looks like you have), try putting in a with "white-space: pre" style.

i.e:

<div style="white-space: pre;">
  <xsl:value-of select="value"/>
</div>
Hasan Cem Cerit
@Hasan Cem Cerit: You missed *Is it possible to match the cr/lf and replace them with html `<br>`*
Alejandro