Tony Clifton Tony Clifton - 1 year ago 70
HTML Question

Add a linebreak in an HTML table via XSL

I transform an XML file to HTML via XSL.
In my html-table I have a column where I need to break the cell's content to a new line for each entry in one of my xml element-tags.

I'm not sure how to set up the xml-file for this.
Here's the output I was thinking of for my html-file:

<info>
line1<br/>
line2<br/>
line3<br/>
line4
</info>


and what doesn't work for me on the xsl-part:

<td>
<xsl:for-each select="info">
<xsl:value-of select="." /> <br/>
</xsl:for-each>
</td>


I've tried wrapping the lines in the xml with
<ul>
and
<li>
but that didn't work. I think it's the xsl-part where I have the error. If I put a
<br/>
after the value-of statement it will output everything and then add the
<br/>
but of course I want the
<br/>
for each entry.

I hope you can tell me of a way how to set this up correctly.

Answer Source

This transformation:

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

 <xsl:template match="info/*[position() > 1]">
  <br /><xsl:text>&#xA;</xsl:text>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document (as no source XML document is provided in the question):

<info>
  <t>line1</t>
  <t>line2</t>
  <t>line3</t>
  <t>line4</t>
  <t>line5</t>
</info>

produces the wanted result (<info> intentionally not generated as this isn't an HTML element):

line1<br/>
line2<br/>
line3<br/>
line4<br/>
line5

Note 1: This transformation was verified to produce exactly the above result with all of my XSLT 1.0 and 2.0 processors: MSXML3, MSXML4, MSXML6, .NET XslCompiledTransform, .NET XslTransform, Saxon 6.5.4, XML-SPY, Saxon 9.1.05, XQSharp, AltovaXML2011 (XML-SPY for XSLT 2.0).

Note 2: If you really want to have the result wrapped int an <info> element, just add this template:

 <xsl:template match="info">
  <info>
   <xsl:text>&#xA;</xsl:text><xsl:apply-templates/><xsl:text>&#xA;</xsl:text>
  </info>
 </xsl:template>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download