Tables

In this lesson of the XSL-FO tutorial, you will learn...
  1. To create tables.
  2. To format tables.

Creating Tables

The syntax for creating a table in XSL-FO is as follows.

Code Sample: Tables/Demos/TableSyntax.fo

<fo:table table-layout="fixed" border-width="1mm" border-style="solid">
 <fo:table-column column-width="3in"/>
 <fo:table-column column-width="3in"/>
 <fo:table-header text-align="center" background-color="silver">
  <fo:table-row>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block font-weight="bold">Header</fo:block>
   </fo:table-cell>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block font-weight="bold">Header</fo:block>
   </fo:table-cell>
  </fo:table-row>
 </fo:table-header>
 <fo:table-body>
  <fo:table-row>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>Content</fo:block>
   </fo:table-cell>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>Content</fo:block>
   </fo:table-cell>
  </fo:table-row>
  <fo:table-row>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>Content</fo:block>
   </fo:table-cell>
   <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
    <fo:block>Content</fo:block>
   </fo:table-cell>
  </fo:table-row>
 </fo:table-body>
</fo:table>
Code Explanation

Let's examine this element by element.

fo:table

The fo:table element contains the whole table. Common attributes include table-layout (fixed, auto, or inherit) and attributes controlling the borders (e.g, border-width) and background (e.g, background-color) of the table.

fo:table-column

The fo:table-column elements are used to set the width of the column. The key attribute is column-width. The fo:table-column elements are the first children of the fo:table element and there should be one fo:table-column element for each column in the table.

fo:table-header, fo:table-body, fo:table-footer

The table is vertically divided into a fo:table-header, fo:table-body, and fo:table-footer. Only the fo:table-body is required. These elements are then divided into rows with the fo:table-row element.

fo:table-row

The fo:table-row element is divided into one or more cells with fo:table-cell elements. The total number of cells in each row should equal the number of fo:table-column elements in the table, though cells can span columns, in which case they count for however many columns they span.

fo:table-cell

The fo:table-cell element contains the content of the cell, which is displayed in the output.

Let's look at an example.

Code Sample: Tables/Demos/Beatles.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output method="xml" indent="yes"/>
 <xsl:template match="/beatles">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
    <fo:simple-page-master master-name="table" page-height="11in" 
     page-width="8.5in" margin-top=".5in" margin-bottom=".5in" 
     margin-left=".5in" margin-right=".5in">
     <fo:region-body margin-top="1in" margin-bottom="1in"/>
     <fo:region-before extent=".5in"/>
     <fo:region-after extent=".5in"/>
    </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="table">
    <fo:static-content flow-name="xsl-region-before" text-align="center">
     <fo:block font-size="20pt" font-weight="bold">
      The Beatles
     </fo:block>
    </fo:static-content>
    <fo:static-content flow-name="xsl-region-after">
     <fo:block text-align="right" font-size="9pt">
      Image borrowed from 
      <fo:basic-link external-destination="url('
http://www.northwestern.edu/observer/issues/2003-06-05/images/beatles.jpg')"
      color="blue">here</fo:basic-link>.
     </fo:block>
    </fo:static-content>
    
<fo:flow flow-name="xsl-region-body">
 <fo:block>
  <fo:external-graphic src="url('beatles.jpg')" width="340px" height="238px"/>
 </fo:block>
 <fo:table table-layout="fixed" border-width="1mm" 
  border-style="solid" font-size="26pt" space-before="20pt">
  <fo:table-column column-width="3in"/>
  <fo:table-column column-width="3in"/>
  <fo:table-header text-align="center" background-color="silver">
   <fo:table-row>
    <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
     <fo:block font-weight="bold">First Name</fo:block>
    </fo:table-cell>
    <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
     <fo:block font-weight="bold">Last Name</fo:block>
    </fo:table-cell>
   </fo:table-row>
  </fo:table-header>
  <fo:table-body>
   <xsl:for-each select="//beatle">
    <fo:table-row>
     <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
      <fo:block>
       <fo:basic-link external-destination="url('{@link}')" color="blue">
        <xsl:value-of select="name/firstname"/>
       </fo:basic-link>
      </fo:block>
     </fo:table-cell>
     <fo:table-cell padding="1mm" border-width="1mm" border-style="solid">
      <fo:block>
       <fo:basic-link external-destination="url('{@link}')" color="blue">
        <xsl:value-of select="name/lastname"/>
       </fo:basic-link>
      </fo:block>
     </fo:table-cell>
    </fo:table-row>
   </xsl:for-each>
  </fo:table-body>
 </fo:table>
</fo:flow>

   </fo:page-sequence>
  </fo:root>
 </xsl:template>
</xsl:stylesheet>
Code Explanation

The result would look like this.

Tables Conclusion

In this lesson of the XSL-FO tutorial, you have seen how to create tables with XSL-FO.

To continue to learn XSL-FO go to the top of this page and click on the next lesson in this XSL-FO Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.