Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 22nd, 2006, 06:29 AM
Registered User
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to insert <br> in a table with this xml/xsl?

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>
        <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>

 
Old September 22nd, 2006, 07:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is what template rules are for.

  <xsl:template match="/">
    <html>
      <body>
        <CENTER>
          <H1>Membershipbenefits</H1>
        </CENTER>
        </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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace "<enter>" with "<br>" Varg_88 Classic ASP Basics 5 February 14th, 2011 12:33 PM
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
<br/> becomes <br></br> Kabe XSLT 1 July 6th, 2007 07:42 AM
to append <br> in XSLT dharus XSLT 4 August 1st, 2006 09:52 AM
Replace vbcrlf with <br> using C# kgriffin ASP.NET 1.0 and 1.1 Basics 2 May 4th, 2005 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.