I’m working on an XSL template to convert an XHTML/hResume document to plain text, and I’m having trouble with the table layout (no, not layout tables). At the moment I’ve got the following, using the excellent Dave Pawson’s padding template:
<variable name="newline" select="' '"/>
<template match="xhtml:table">
<variable name="maxWidth">
<for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td">
<sort select="string-length(child::text()|child::node())" order="descending" data-type="number"/>
<if test="position() = 1">
<value-of select="string-length(child::text()|child::node())"/>
</if>
</for-each>
</variable>
<for-each select="xhtml:tr">
<for-each select="xhtml:th|xhtml:td">
<variable name="string">
<for-each select="child::text()|child::node()">
<value-of select="."/>
</for-each>
</variable>
<value-of select="$string"/>
<call-template name="append-pad">
<with-param name="length" select="$maxWidth - string-length($string)"/>
</call-template>
<text> </text>
</for-each>
<value-of select="$newline"/>
</for-each>
<value-of select="$newline"/>
</template>
This produces columns of equal width, but I’d like to improve it in a couple ways:
- Find and use the max width of each column. For that it’s necessary to store a flexible number of values. I can change maxWidth to do this in the simple cases, but how do you handle spanning columns?
- Center the contents of spanning columns.
Are there any templates to do something like this?
With a “global” (for every cell in table)
$maxWithyou could handle colspans like this stylesheet (preserving your own logic):Input:
Output:
EDIT: In order to center the cells with colspan, use this stylesheet (now with my own logic):
Output: