Re: Re[2]: New html-element: table3

2007/4/24, Dmitry Turin:
>
> >> My interest would be in populating the table with a single round trip
>
> Preston, what defect has the following construction
> ("tc" means "table column"):
>
> <table attribute="special">
> <tr>
>  <th id=10>ColName1</th>
>  <th id=20>ColName2</th>
>  <th id=30>ColName3</th>
> </tr>
> <tc>
>  <th id=1>RowName1</th>
>  <th id=2>RowName2</th>
>  <th id=3>RowName3</th>
> </tc>
> <data>
>  <td row=1 col=10>Data11</td>
>  <td row=1 col=20>Data12</td>
>  <td row=1 col=30>Data13</td>
>  <td row=2 col=10>Data21</td>
>  <td row=2 col=20>Data22</td>
>  <td row=2 col=30>Data23</td>
>  <td row=3 col=10>Data31</td>
>  <td row=3 col=20>Data32</td>
>  <td row=3 col=30>Data33</td>
> </data>
> </table>

You can easily transform this into:
<table>
   <thead>
      <tr>
         <td></td>
         <th id="10">ColName1</th>
         <th id="20">ColName2</th>
         <th id="30">ColName3</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <th id="1">RowName1</th>
         <td>Data11</td>
         <td>Data12</td>
         <td>Data13</td>
      </tr>
      <tr>
         <th id="2">RowName2</th>
         <td>Data21</td>
         <td>Data22</td>
         <td>Data23</td>
      </tr>
      <tr>
         <th id="3">RowName3</th>
         <td>Data31</td>
         <td>Data32</td>
         <td>Data33</td>
      </tr>
   </tbody>
</table>
(assuming you mistakenly used <tc> instead <tr> and <tr> instead of <tc>)

I did so with the following XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html" />

   <!-- identity transformation -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="table[@attribute='special']">
      <xsl:variable name="table" select="." />
      <table>
         <thead>
            <tr>
               <td>
                  <!-- top/left corner -->
               </td>
               <xsl:apply-templates select="tc/th" />
            </tr>
         </thead>
         <tbody>
            <xsl:for-each select="tr/th">
               <tr>
                  <xsl:apply-templates select="."/>

                  <xsl:call-template name="make-row-data-cells">
                     <xsl:with-param name="table" select="$table" />
                     <xsl:with-param name="row" select="@id" />
                  </xsl:call-template>
               </tr>
            </xsl:for-each>
         </tbody>
      </table>
   </xsl:template>

   <xsl:template name="make-row-data-cells">
      <xsl:param name="table" />
      <xsl:param name="row" />

      <xsl:for-each select="$table/tc/th">
         <td>
            <xsl:apply-templates select="$table/data/td[@row=$row and
@col=current()/@id]/node()" />
         </td>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

(far from perfect: for example, it doesn't copy <td> attributes; but
enough for the demonstration)

-- 
Thomas Broyer

Received on Tuesday, 24 April 2007 19:49:55 UTC