HTML到CALS表格? [英] HTML to CALS tables?

查看:157
本文介绍了HTML到CALS表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果我自己做得够多,我会发布它以备将来参考。

解决方案

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

 < xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =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(before-sibling :: td [not(@colspan)])+ 1/>
< / xsl:attribute>
< xsl:attribute name =nameend>
< / 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>

<! - - 后备规则 - >
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()/>
< / xsl:copy>
< / xsl:template>
< / xsl:stylesheet>

有两个棘手的问题。首先,CALS表需要一个包含列数的 group / @ cols 属性。因此,我们需要在XHTML表中查找一行中的最大单元格数量,但我们必须注意 colspan 声明,以便使用 colspan > 1创建正确的列数!我的样式表中的第一个模板基于@Tim C对每行最大单元数问题。另一个问题是,对于多列单元格,XHTML表示这个单元格是3列宽( colspan =3),而CALS会说这个单元格在第2列开始,并在第4列结束( namest =2nameend =4 >)。这个转换是在样式表中的第二个模板中完成的。

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


I'm checking to see if anyone has an XSLT laying around that transforms HTML tables to CALS. I've found a lot of material on going the other way (CALS to HTML), but not from HTML. I thought somebody may have done this before so I don't have to reinvent the wheel. I'm not looking for a complete solution. Just a starting point.

If I get far enough on my own, I'll post it for future reference.

解决方案

I've come up with a much simpler solution than what @Flack linked to:

<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>

There are two tricky points. First, a CALS table needs a tgroup/@cols attribute containing the number of columns. So we need to find the maximum number of cells in one row in the XHTML table - but we must heed colspan declarations so that a cell with colspan > 1 creates the right number of columns! The first template in my stylesheet does just that, based on @Tim C's answer to the max cells per row problem.

Another problem is that for multi-column cells XHTML says "this cell is 3 columns wide" (colspan="3") while CALS will say "this cell starts in column 2 and ends in column 4" (namest="2" nameend="4"). That transformation is done in the second template in the stylesheet.

The rest is indeed fairly straightforward. The stylesheet doesn't deal with details like changing style="width: 50%" into width="50%" etc. but those are relatively common problems, I believe.

这篇关于HTML到CALS表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆