Simple tables

In this example a simple table is generated.

It shows how the table concept can be supported in the form of a minimal basic framework. The various characteristics available for the formatting of a table are not pointed out here. However, they can be integrated according to requirement without any problem.

The following example shows a HTML table. This table consists of a header and a body, each with one row containing two cells.

Excerpt from the XML instance:

<table>
   <thead>
      <tr>
         <td>Head 1</td>
         <td>Head 2</td>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Cell 1</td>
         <td>Cell 2</td>
      </tr>
   </tbody>
</table>

Stylesheet:

<xsl:template match="table">
   <fo:table>
      <xsl:apply-templates/>                                                          ❶
   </fo:table>
</xsl:template>
...
<xsl:template match="thead">
   <fo:table-header>                                                                  ❷
      <xsl:apply-templates/>                                                          ❷
   </fo:table-header>
</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">                          ❺ 
      <fo:block>
         <xsl:apply-templates/>                                                       ❺
      </fo:block>
   </fo:table-cell>
</xsl:template>
...
<xsl:template match="thead/tr/td">
   <fo:table-cell border-style="solid" border-width="0.5mm" background-color="green"> ❻
      <fo:block>                                                                      ❻
         <xsl:apply-templates/>
      </fo:block>
   </fo:table-cell>
</xsl:template>

❶ In order to generate a table without a headline or caption, the <fo:table> element is used as a top element. This element encloses the entire content of the table.

❷ With the <fo:table-header> element the table header is generated. The header is filled with the corresponding contents by <xsl:apply-templates>. If a table is wrapped on a next page, this table header is automatically repeated on the following page.

❸ The <fo:table-body> element contains the table body.

❹ The table rows are defined in the <fo:table-row> element.

❺ With <fo:table-cell> a table cell which contains the real content is generated. The attributes border-style and border-width generate the solid borders around the cells. The borders for the top, the bottom, the right and the left could also be separately defined in order to specifiy differently, for example, column or row lines.

❻ In the closing template the cells for the table header are defined. These differ from the ones in the table body by the background colour determined with the background-color attribute.

 

<< back next >>

Copyright © dpunkt.verlag GmbH 2004
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSL-FO in der Praxis" ("XSL-FO in practice") is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo@dpunkt.de