Re: [xsl] Convert CALS Table

Subject: Re: [xsl] Convert CALS Table
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 10 Oct 2007 19:17:37 +0100
On 10/10/2007, Owens, Stephen P <Stephen.P.Owens@xxxxxxxxxx> wrote:
> Hello gregor FELLENZ:
>
> --- In your original inquiry you said -
> >hello xsl-list,
> >
> >i have to normalize CALS tables, to another table model which has no
> >colspan resp. rowspan abilities.
> >
> >I have to fill for each omitted entry an empty cell in my new
> >document, to generate valid tables.
> >
> >Realizing this for colspan is no problem (as you can see in my
> >xslt) but i have no idea how to cover the rowspan.
> >The problem is, that i have to insert in every spanned row an empty
> >cell, but how can i gather this information?
> >
> >Any ideas or hints how i can realize this?
> --- End of quote -
>
> There is a response to your question posted at
> http://www.integrityintegrators.net/blog/?p=1
>
> If you are still interested.

I must have missed that one too.... anyway I wrote a table normalizer
a while back:

http://andrewjwelch.com/code/xslt/table/table-normalization.html

In case that link stops working one day here's the code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; version="2.0"
exclude-result-prefixes="xs">

<xsl:output indent="yes" omit-xml-declaration="yes" />

<xsl:variable name="table_with_no_colspans">
    <xsl:apply-templates mode="colspan" />
</xsl:variable>

<xsl:variable name="table_with_no_rowspans">
    <xsl:for-each select="$table_with_no_colspans">
        <xsl:apply-templates mode="rowspan" />
    </xsl:for-each>
</xsl:variable>

<xsl:template match="/">
    <xsl:apply-templates select="$table_with_no_rowspans" mode="final" />
</xsl:template>

<xsl:template match="@*|*" mode="#all">
    <xsl:copy>
        <xsl:apply-templates select="@*|*" mode="#current" />
    </xsl:copy>
</xsl:template>

<xsl:template match="td" mode="colspan">
    <xsl:choose>
        <xsl:when test="@colspan">
            <xsl:variable name="this" select="." as="element()" />
            <xsl:for-each select="1 to @colspan">
                <td>
                    <xsl:copy-of select="$this/@*[not(name() =
'colspan')][not(name() = 'width')]" />
                    <xsl:copy-of select="$this/node()" />
                </td>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="." />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- make sure it works for both table/tr and table/tbody/tr -->
<xsl:template match="tbody|table[not(tbody)]" mode="rowspan">
    <xsl:copy>
        <xsl:copy-of select="tr[1]" />
        <xsl:apply-templates select="tr[2]" mode="rowspan">
            <xsl:with-param name="previousRow" select="tr[1]" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="tr" mode="rowspan">
    <xsl:param name="previousRow" as="element()" />

    <xsl:variable name="currentRow" select="." />

    <xsl:variable name="normalizedTDs">
        <xsl:for-each select="$previousRow/td">
            <xsl:choose>
                <xsl:when test="@rowspan &gt; 1">
                    <xsl:copy>
                        <xsl:attribute name="rowspan">
                            <xsl:value-of select="@rowspan - 1" />
                        </xsl:attribute>
                        <xsl:copy-of select="@*[not(name() = 'rowspan')]" />
                        <xsl:copy-of select="node()" />
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$currentRow/td[1 +
count(current()/preceding-sibling::td[not(@rowspan) or (@rowspan =
1)])]" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="newRow" as="element(tr)">
        <xsl:copy>
            <xsl:copy-of select="$currentRow/@*" />
            <xsl:copy-of select="$normalizedTDs" />
        </xsl:copy>
    </xsl:variable>

    <xsl:copy-of select="$newRow" />

    <xsl:apply-templates select="following-sibling::tr[1]" mode="rowspan">
        <xsl:with-param name="previousRow" select="$newRow" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="td" mode="final">
    <xsl:choose>
        <xsl:when test="@rowspan">
            <xsl:copy>
                <xsl:copy-of select="@*[not(name() = 'rowspan')]" />
                <xsl:copy-of select="node()" />
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="." />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>



cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread