XML technologies / XSL-FO / Examples of application / Complex tables

 

Complex tables

In this example a table is generated with spannings of rows and columns.

Here, the basic framework of a table being introduced in the last chapter is expanded by the spanning of rows and columns. The principle shown in this example for reading out typographical features from the XML instance can be used for many table features and adapted to the specific requirements.

The following example shows a HTML table. This table consists of a table body with four rows. The first row has two cells. In the second row one cell spans both columns. In the third and fourth row the first of two cells spans both rows.

Excerpt from the XML instance:

<table>
   <tbody>
      <tr>
         <td>Cell 1</td>
         <td>Cell 2</td>
      </tr>
      <tr>
         <td colspan="2">Cell 3</td>
      </tr>
      <tr>
         <td rowspan="2">Cell 4</td>
         <td>Cell 5</td></tr>
      <tr>
         <td>Cell 6</td></tr>
   </tbody>
</table>

Stylesheet:

<xsl:template match="table"> 
   <fo:table>
      <xsl:apply-templates/>
   </fo:table>
</xsl:template>
<xsl:template match="tbody">
   <fo:table-body>
      <xsl:apply-templates/> 
   </fo:table-body>
</xsl:template>
<xsl:template match="tr">
   <fo:table-row>
      <xsl:apply-templates/>
   </fo:table-row>
</xsl:template>
<xsl:template match="td">                                        ❶  
   <fo:table-cell border-style="solid" border-width="0.5mm">     ❶
      <xsl:attribute name="number-columns-spanned">              ❷
         <xsl:value-of select="@colspan"/>                       ❷
      </xsl:attribute>
      <xsl:attribute name="number-rows-spanned">                 ❸
         <xsl:value-of select="@rowspan"/>                       ❸
      </xsl:attribute>
      <fo:block>
         <xsl:apply-templates/>
      </fo:block>
   </fo:table-cell>
</xsl:template>

❶ The template generates the table cells. The <fo:table-cell> element determines the border characteristics of the cell with the attributes border-style and border-width.

❷ With the number-columns-spanned attribute columns are spanned. The value determines the number of columns to be spanned. The <xsl:attribute> element generates an attribute in the <fo:table-cell> element with the name of the value being indicated in the name attribute, in this case the number-columns-spanned attribute. The value of the attribute is read out from the instance with <xsl:value-of>.

❸ With the number-rows-spanned attribute rows are spanned. The procedure corresponds to the one for number-columns-spanned.

 

back to top