HTML到CALS表格? [英] HTML to CALS tables?
问题描述
我正在检查是否有任何人将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表需要一个包含列数的
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屋!