RE: [xsl] FW: xsl code to create tables of different number columns and rows

Subject: RE: [xsl] FW: xsl code to create tables of different number columns and rows
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Fri, 20 May 2005 15:57:08 +0000
Martie,

columns, and in the second table 2. Throughout the book there can be a number of tables with different amount of columns/rows. Thus, I have to create a template that can interpret the cols="?" attribute and for each instance create a table with that amount of columns.

If your data is consistent this is simple. However this may be an unsafe assumption. At some point you may also want/need to handle cases in which the number of <entry> doesn't match number of <colspec>.


I am not good (yet) with xml or html. If I create an html table first, how do I then apply the tgroup/@cols to that?

Hopefully this will help along your way to proficiency:


This XML (yours corrected to have a single overall containing element):

<data>
   <table>
       <tgroup cols="3">
           <colspec colname="col1"/>
           <colspec colname="col2"/>
           <colspec colname="col3"/>
           <tbody>
               <row>
                   <entry>column1rowa</entry>
                   <entry>column2rowa</entry>
                   <entry>column3rowa</entry>
               </row>
               <row>
                   <entry>column1rowb</entry>
                   <entry>column2rowb</entry>
                   <entry>column3rowb</entry>
               </row>
           </tbody>
       </tgroup>
   </table>
   <table>
       <tgroup cols="2">
           <colspec colname="col1"/>
           <colspec colname="col2"/>
           <tbody>
               <row>
                   <entry>column1rowa</entry>
                   <entry>column2rowa</entry>
               </row>
               <row>
                   <entry>column1rowb</entry>
                   <entry>column2rowb</entry>
               </row>
               <row>
                   <entry>column1rowc</entry>
                   <entry>column2rowc</entry>
               </row>
           </tbody>
       </tgroup>
   </table>
</data>

With this XSL:

<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>


   <xsl:template match="/">
       <data>
       <xsl:apply-templates select="data/table"/>
       </data>
	</xsl:template>

   <xsl:template match="table">
       <table>
           <tr>
           <xsl:apply-templates select="tgroup/colspec"/>
           </tr>
           <xsl:apply-templates select="tgroup/tbody/row"/>
       </table>
	</xsl:template>

   <xsl:template match="row">
       <tr>
           <xsl:apply-templates select="entry"/>
       </tr>
	</xsl:template>

   <xsl:template match="colspec">
       <th><xsl:value-of select="@colname"/></th>
	</xsl:template>

   <xsl:template match="entry">
       <td><xsl:value-of select="."/></td>
	</xsl:template>

</xsl:stylesheet>

produces:

<?xml version="1.0" encoding="UTF-8"?>
<data>
 <table>
   <tr>
     <th>col1</th>
     <th>col2</th>
     <th>col3</th>
   </tr>
   <tr>
     <td>column1rowa</td>
     <td>column2rowa</td>
     <td>column3rowa</td>
   </tr>
   <tr>
     <td>column1rowb</td>
     <td>column2rowb</td>
     <td>column3rowb</td>
   </tr>
 </table>
 <table>
   <tr>
     <th>col1</th>
     <th>col2</th>
   </tr>
   <tr>
     <td>column1rowa</td>
     <td>column2rowa</td>
   </tr>
   <tr>
     <td>column1rowb</td>
     <td>column2rowb</td>
   </tr>
   <tr>
     <td>column1rowc</td>
     <td>column2rowc</td>
   </tr>
 </table>
</data>

You may want to make changes to this, starting with changing the output type to "html".

Regards,

--A

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar  get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


Current Thread