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

 

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