HTML到CALS表?

Jef*_*eff 5 html xslt html-table

我正在检查是否有人将XSLT转换为将HTML表格转换为CALS.我发现了很多关于其他方式(CALS到HTML)的材料,但不是来自HTML.我以为有人可能会这样做,所以我不必重新发明轮子.我不是在寻找一个完整的解决方案.只是一个起点.

如果我自己走得足够远,我会发布它以供将来参考.

小智 4

我想出了一个比 @Flack 链接的更简单的解决方案:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="tbody">
    <xsl:variable name="maxColumns">
        <xsl:for-each select="tr">
            <xsl:sort select="sum(td/@colspan) + count(td[not(@colspan)])" data-type="number"/>
            <xsl:if test="position() = last()">
                <xsl:value-of select="sum(td/@colspan) + count(td[not(@colspan)])"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <tgroup>
        <xsl:attribute name="cols">
            <xsl:value-of select="$maxColumns"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </tgroup>
</xsl:template>

<xsl:template match="td[@colspan > 1]">
    <entry>
        <xsl:attribute name="namest">
            <xsl:value-of select="sum(preceding-sibling::td/@colspan) + count(preceding-sibling::td[not(@colspan)]) + 1"/>
        </xsl:attribute>
        <xsl:attribute name="nameend">
            <xsl:value-of select="sum(preceding-sibling::td/@colspan) + count(preceding-sibling::td[not(@colspan)]) + @colspan"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*[name() != 'colspan']|node()"/>
    </entry>
</xsl:template>

<xsl:template match="tr">
    <row>
        <xsl:apply-templates select="@*|node()"/>
    </row>
</xsl:template>

<xsl:template match="td">
    <entry>
        <xsl:apply-templates select="@*|node()"/>
    </entry>
</xsl:template>

<xsl:template match="td/@rowspan">
    <xsl:attribute name="morerows">
        <xsl:value-of select=". - 1"/>
    </xsl:attribute>
</xsl:template>

<!-- fallback rule -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

有两个棘手的点。首先,CALS 表需要一个包含列数的tgroup/@cols属性。因此,我们需要找到 XHTML 表中一行的最大单元格数 - 但我们必须注意colspan声明,以便colspan > 1 的单元格创建正确的列数!我的样式表中的第一个模板就是这样做的,基于 @Tim C 对每行最大单元格问题的回答。

另一个问题是,对于多列单元格,XHTML 表示“此单元格宽 3 列”( colspan="3" ),而 CALS 会表示“此单元格从第 2 列开始,到第 4 列结束”( namest="2" nameend =“4”)。该转换是在样式表的第二个模板中完成的。

其余的确实相当简单。样式表不处理诸如将style="width: 50%"更改为width="50%"等细节,但我相信这些是相对常见的问题。