[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] CALS table model to HTML table conversion


Subject: Re: [xsl] CALS table model to HTML table conversion
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Tue, 24 May 2005 14:47:43 +0200

Hi,

> > Can anybody point those places where I can get these stylesheets.
>
>
> It's not particularly good or complete (CALS is very heavyweight) but
> you may get some ideas from it.

Heavily inspired by Andrew Welch's solution, which was too complicated
for me, I made a simpler one myself that was good enough for my needs.
Perhaps this will also be of help.

I had to make some more tests in the entry template as @valign in
colspan elements is not working probably in Mozilla 1.00 (I have not
tested it against later versions, so simplification might be
possible). In IE. 6.1 you will not need to inherit @valign, but simply
make an almost 1-1 transformation to HTML 4.0 tables.

As with Andrew Welch's solution, it requires a CSS that recognises t,
b, l and r classes.

Regards,
Ragulf Pickaxe :-)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>

<xsl:template match="table">
<xsl:variable name="Class">
<xsl:choose>
<xsl:when test="@frame='sides'">l r</xsl:when>
<xsl:when test="@frame='top'">t</xsl:when>
<xsl:when test="@frame='bottom'">b</xsl:when>
<xsl:when test="@frame='topbot'">t b</xsl:when>
<xsl:when test="@frame='all'">t b l r</xsl:when>
<xsl:when test="@frame='none'"></xsl:when>
<xsl:otherwise>t b l r</xsl:otherwise>	<!-- default -->
</xsl:choose>
</xsl:variable>

<Table class="{$Class}">
<xsl:for-each select="@*">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:apply-templates select="tgroup"/>
</Table>
</xsl:template>

<xsl:template match="tgroup">
<xsl:variable name="total-percents-colwidth"><xsl:call-template
name="total-width"/></xsl:variable>
<colgroup>
<xsl:apply-templates select="colspec">
<xsl:with-param name="total-percents-colwidth"
select="$total-percents-colwidth"/>
</xsl:apply-templates>
</colgroup>

<xsl:apply-templates select="thead"/>
<xsl:apply-templates select="tbody"/>

</xsl:template>

<xsl:template name="total-width">
<xsl:param name="percents"
select="colspec[contains(@colwidth,'*')]/@colwidth"/>
<xsl:param name="total" select="'0'"/>
<xsl:choose>
<xsl:when test="count($percents)&gt;1">
<xsl:call-template name="total-width">
<xsl:with-param name="percents" select="$percents[position()&gt;1]"/>
<xsl:with-param name="total"
select="number($total)+number(substring-before($percents[1],'*'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of
select="number($total)+number(substring-before($percents[1],'*'))"/></xsl:oth
erwise>
</xsl:choose>
</xsl:template>

<xsl:template match="colspec">
<xsl:param name="total-percents-colwidth" select="'1'"/>
<xsl:choose>
<xsl:when test="contains(@colwidth,'pt')">
<col width="{substring-before(@colwidth,'pt')}" />
</xsl:when>
<xsl:when test="contains(@colwidth,'*')">
<col width="{100 * number(substring-before(@colwidth,'*')) div
number($total-percents-colwidth)}%" />
</xsl:when>
<xsl:otherwise>
<col />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="thead">
<thead>
<xsl:apply-templates select="row"/>
</thead>
</xsl:template>

<xsl:template match="tbody">
<tbody>
<xsl:apply-templates select="row"/>
</tbody>
</xsl:template>

<xsl:template match="thead/row">
<tr>
<xsl:apply-templates select="entry">
<xsl:with-param name="up-rowsep">
<xsl:choose>
<xsl:when test="@rowsep"><xsl:value-of select="@rowsep"/></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="td" select="'th'"/>
</xsl:apply-templates>
</tr>
</xsl:template>

<xsl:template match="tbody/row">
<tr>
<xsl:apply-templates select="entry">
<xsl:with-param name="up-rowsep">
<xsl:choose>
<xsl:when test="@rowsep"><xsl:value-of select="@rowsep"/></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:apply-templates>
</tr>
</xsl:template>

<xsl:template match="entry">
<xsl:param name="up-rowsep"/>
<xsl:param name="td" select="'td'"/>
<xsl:variable name="align">
<xsl:choose>
<xsl:when test="@align"><xsl:value-of select="@align"/></xsl:when>
<xsl:when test="ancestor::tgroup[1]/colspec[position()]/@align"><xsl:value-of
select="ancestor::tgroup[1]/colspec[position()]/@align"/></xsl:when>
<xsl:when test="ancestor::tgroup[1]/@align"><xsl:value-of
select="ancestor::tgroup[1]/@align"/></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="valign">
<xsl:choose>
<xsl:when test="@valign"><xsl:value-of select="@valign"/></xsl:when>
<xsl:when test="row/@valign"><xsl:value-of select="row/@valign"/></xsl:when>
<xsl:when test="parent::tbody/@valign"><xsl:value-of
select="parent::tbody/@valign"/></xsl:when>
<xsl:when test="parent::thead/@valign"><xsl:value-of
select="parent::thead/@valign"/></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:element name="{$td}">
<xsl:if test="@namest">
<xsl:attribute name="colspan"><xsl:value-of
select="number(@nameend)-number(@namest)+1"/></xsl:attribute>
</xsl:if>
<xsl:if test="@morerows">
<xsl:attribute name="rowspan"><xsl:value-of
select="number(@morerows)+1"/></xsl:attribute>
</xsl:if>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="@rowsep='0'"></xsl:when>
<xsl:when test="../following-sibling::row">
<xsl:choose>
<xsl:when test="@rowsep='1' or $up-rowsep='1'">b </xsl:when>
<xsl:when test="ancestor::tgroup/colspec[position()]/@rowsep='1'">b
</xsl:when>
<xsl:when test="ancestor::tgroup/@rowsep='1'">b </xsl:when>
<xsl:when test="ancestor::table/@rowsep='1'">b </xsl:when>
</xsl:choose>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:choose
<xsl:when test="@colsep='0'"></xsl:when>
<xsl:when test="following-sibling::entry">
<xsl:choose>
<xsl:when test="@colsep='1'">r </xsl:when>
<xsl:when test="ancestor::tgroup/colspec[position()]/@colsep='1'">r
</xsl:when>
<xsl:when test="ancestor::tgroup/@colsep='1'">r </xsl:when>
<xsl:when test="ancestor::table/@colsep='1'">r </xsl:when>
</xsl:choose>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="$valign!=''">
<xsl:attribute name="valign">
<xsl:value-of select="$valign"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$align!=''">
<xsl:attribute name="align">
<xsl:value-of select="$align"/>
</xsl:attribute>
</xsl:if>

<xsl:apply-templates/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>


Current Thread