Subject: How to insert <br> in a table with this xml/xsl?
Posted By: andigra Post Date: 9/22/2006 6:29:56 AM
I have an XML with a template <br></br> which I would like to convert into an html linebreak (<br>), so that the benefittext shown in the xml below is seperated by a linebreak each time the template <br></br> occurs. The template can be manipulated by code before the xml is written, so if you suggest I rewrite the xml with regards to this template then that is no problem.

My xml:

<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="Css/exampleA.xsl"?>
<NewDataSet>
  <Table>
    <BenefitText>- 10 % discount on sightseeing by bus or boat in London<br></br>
- 10 % discount on sightseeing by bus or boat in Stockholm<br></br>
- 10 % discount on sightseeing by bus or boat in Helsinki<br></br>
- 10 % discount on sightseeing by bus or boat in Copenhagen</BenefitText>
    <BenefitCompanyName>Travels Inc.</BenefitCompanyName>
    <MembershipCompanyName>Happy Memberships</MembershipCompanyName>
    <Categories>Services</Categories>
  </Table>
</NewDataSet>

My xsl:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <CENTER>
          <H1>Membershipbenefits</H1>
        </CENTER>
        <HR WIDTH="50%" SIZE="3"></HR>
        <table border="1" bgcolor="white">
          <tr>
            <th>Membership</th>
            <th>Company</th>
            <th>Benefit</th>
            <th>Category</th>
          </tr>
          <xsl:for-each select="NewDataSet/Table">
            <tr>
              <td>
                <xsl:value-of select="MembershipCompanyName"/>
              </td>
              <td>
                <xsl:value-of select="BenefitCompanyName"/>
              </td>
              <td>
                <xsl:value-of select="BenefitText"/>
        <br>
        <xsl:value-of select="//br"/>
        </br>
              </td>
              <td>
                <xsl:value-of select="Categories"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>


</xsl:stylesheet>

Reply By: mhkay Reply Date: 9/22/2006 7:01:43 AM
This is what template rules are for.

  <xsl:template match="/">
    <html>
      <body>
        <CENTER>
          <H1>Membershipbenefits</H1>
        </CENTER>
        <HR WIDTH="50%" SIZE="3"></HR>
        <table border="1" bgcolor="white">
          <tr>
            <th>Membership</th>
            <th>Company</th>
            <th>Benefit</th>
            <th>Category</th>
          </tr>
          <xsl:apply-templates select="NewDataSet/Table"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Table">
            <tr>
              <td>
                <xsl:apply-templates select="MembershipCompanyName"/>
              </td>
              <td>
                <xsl:apply-templates  select="BenefitCompanyName"/>
              </td>
              <td>
                <xsl:apply-templates  select="BenefitText"/>
              </td>
              <td>
                <xsl:apply-templates select="Categories"/>
              </td>
            </tr>
   </xsl:template>

   <xsl:template match="br">
      <br/>
   </xsl:template>

You don't need any other rules: the defaults do the right thing.

Also remember to use <xsl:output method="html"/> to get the <br> element formatted correctly for HTML output.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Go to topic 49999

Return to index page 168
Return to index page 167
Return to index page 166
Return to index page 165
Return to index page 164
Return to index page 163
Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159