Docbook - Table

By xngo on June 18, 2019

Long table

<!-- Long table that can't be fit into 1 page will be broken down to span across multiple pages -->
<!-- More info can be found at http://www.sagehill.net/docbookxsl/PageBreaking.html#KeepTogetherPI -->
<xsl:attribute-set name="formal.object.properties">
  <xsl:attribute name="keep-together.within-column">auto</xsl:attribute>
</xsl:attribute-set>

Format table headers

<!-- Header rows have blue background and white character color-->
<xsl:template name="table.row.properties">
  <xsl:if test="ancestor::thead">
    <xsl:attribute name="font-weight">bold</xsl:attribute>
    <xsl:attribute name="color">#FFFFFF</xsl:attribute><!-- White -->
    <xsl:attribute name="background-color">#0000FF</xsl:attribute><!-- Blue -->
  </xsl:if>
  <xsl:attribute name="text-align">left</xsl:attribute><!-- left, right, justify -->
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Format table header
Date: 2008-01-17:
Author: Xuan Ngo
-->
<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" encoding="UTF-8" indent="yes"/>
 
  <!-- Copy everything -->
  <!-- ############# -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
 
  <!-- Catching matching conditions -->
  <xsl:template match="thead/row">
    <xsl:element name="row">
      <xsl:processing-instruction name="dbhtml">bgcolor="#000000"</xsl:processing-instruction>
      <xsl:processing-instruction name="dbfo">bgcolor="#000000"</xsl:processing-instruction>
 
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
 
</xsl:stylesheet>


Column width

Add colspec tag inside your xml DocBook file.

<table><title>My table</title>
<tgroup cols="4" >
<colspec colnum="1" colname="col1" colwidth="1*"/>
<colspec colnum="2" colname="col2" colwidth="2*"/>
<colspec colnum="3" colname="col3" colwidth="1.5*"/>
<colspec colnum="4" colname="col4" colwidth="1*"/>
<thead>
...

Here is how you specify column width values: colwidth specifies the desired width of the relevant column. It can be either a fixed measure using one of the CALS units (36pt, 10pc, etc.) or a proportional measure. Proportional measures have the form “number”, meaning this column should be number times wider than a column with the measure “1” (or just “”). These two forms can be mixed, as in “3+1pc”.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.