Tables come in all shapes and sizes and therein lies the problem. The example here employs a relatively short section of XSL code (about 100 lines) to transform a table of any size and configuration — including rowspans and colspans — into the desired HTML table. It uses DocBook elements to describe the table in the XML file.

Example of a complex table rendered from XML

The Challenge of Tables in XML

HTML tables are complex structures, and transforming them from XML presents particular challenges:

• Variable dimensions — tables vary from simple two-column lists to complex grids with dozens of rows and columns.
• Spanning cells — rowspan and colspan attributes must be calculated and applied correctly, or the table renders incorrectly in the browser.
• Header rows and columns — identifying and styling header cells (<th>) differently from data cells (<td>) requires careful XSL logic.

The XML/XSL Solution

The table is described in the XML file using standard DocBook table elements (<table>, <thead>, <tbody>, <row>, <entry>) which provide a well-established, semantic vocabulary for tabular data.

The XSL stylesheet (approximately 100 lines, fully commented) then:

• Iterates over rows and entries using <xsl:for-each> to generate the HTML table structure.
• Calculates spanning by reading DocBook morerows and namest/nameend attributes and converting them to HTML rowspan and colspan values.
• Applies header styling to <thead> rows automatically.
• Handles any table size — the same 100-line XSL works for a 2×2 table or a 20×30 table with complex spanning.

The code is fully commented and free to download and adapt for your own use (retain the copyright notice at the top of each file).

← Back to Footnotes    Back to Code Samples