我有一堆使用CALS模型标记的XML表,其中一些有我想要去掉的空列。下面是一些示例标记
<table frame="none">
<tgroup cols="4" colsep="0" rowsep="0">
<colspec colname="1" colnum="1" colwidth="75pt"/>
<colspec colname="2" colnum="2" colwidth="63pt" align="center"/>
<colspec colname="3" colnum="3" colwidth="63pt" align="center"/>
<colspec colname="4" colnum="4" colwidth="63pt"/>
<thead>
<row valign="bottom">
<entry> </entry>
<entry>No. 9</entry>
<entry>No. 10</entry>
<entry> </entry>
</row>
</thead>
<tbody>
<row>
<entry>Max. size:</entry>
<entry>10.5 m.</entry>
<entry>6.7 m.</entry>
<entry> </entry>
</row>
<row>
<entry>Length:</entry>
<entry>210 m.</entry>
<entry>100 m.</entry>
<entry> </entry>
</row>
<row>
<entry>Depth:</entry>
<entry>11.0</entry>
<entry>7.0</entry>
<entry> </entry>
</row>
</tbody>
</tgroup>
</table>所以我想要完全删除上面例子中的第四列。在许多(大多数?)案例:它将是最后一个专栏,但它不会总是。
您将注意到,第4列确实包含空格,或者可能包含&# 160;个字符。
那么,如何使用xslt删除这些完整的列呢?
提亚
Feargal
发布于 2013-02-08 01:34:58
请尝试一下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- This key will allow us to select all the entries in a column based on their
column number -->
<xsl:key name="kColumn" match="entry"
use="count(. | preceding-sibling::entry)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="tgroup">
<xsl:copy>
<xsl:apply-templates select="@*" />
<!-- Select colspecs whose column isn't all blank -->
<xsl:apply-templates
select="colspec[key('kColumn', position())[normalize-space(.)]]" />
<xsl:apply-templates select="node()[not(self::colspec)]" />
</xsl:copy>
</xsl:template>
<xsl:template match="colspec">
<colspec colname="{position()}" colnum="{position()}">
<xsl:apply-templates
select="@*[local-name() != 'colname' and local-name() != 'colnum']" />
<xsl:apply-templates select="node()" />
</colspec>
</xsl:template>
<!-- Omit entries that belong to all-blank columns -->
<xsl:template match="entry[not(key('kColumn', position())[normalize-space(.)])]" />
</xsl:stylesheet>除了删除空列之外,它还负责对保留的列进行重新编号(我假设您希望这样做),因此使用以下输入,其中第二列为空:
<table frame="none">
<tgroup cols="4" colsep="0" rowsep="0">
<colspec colname="1" colnum="1" colwidth="75pt"/>
<colspec colname="2" colnum="2" colwidth="63pt" align="center"/>
<colspec colname="3" colnum="3" colwidth="63pt" align="center"/>
<colspec colname="4" colnum="4" colwidth="63pt"/>
<thead>
<row valign="bottom">
<entry> </entry>
<entry> </entry>
<entry>No. 9</entry>
<entry>No. 10</entry>
</row>
</thead>
<tbody>
<row>
<entry>Max. size:</entry>
<entry> </entry>
<entry>10.5 m.</entry>
<entry>6.7 m.</entry>
</row>
<row>
<entry>Length:</entry>
<entry> </entry>
<entry>210 m.</entry>
<entry>100 m.</entry>
</row>
<row>
<entry>Depth:</entry>
<entry> </entry>
<entry>11.0</entry>
<entry>7.0</entry>
</row>
</tbody>
</tgroup>
</table>结果是:
<table frame="none">
<tgroup cols="4" colsep="0" rowsep="0">
<colspec colname="1" colnum="1" colwidth="75pt" />
<colspec colname="2" colnum="2" colwidth="63pt" align="center" />
<colspec colname="3" colnum="3" colwidth="63pt" />
<thead>
<row valign="bottom">
<entry> </entry>
<entry>No. 9</entry>
<entry>No. 10</entry>
</row>
</thead>
<tbody>
<row>
<entry>Max. size:</entry>
<entry>10.5 m.</entry>
<entry>6.7 m.</entry>
</row>
<row>
<entry>Length:</entry>
<entry>210 m.</entry>
<entry>100 m.</entry>
</row>
<row>
<entry>Depth:</entry>
<entry>11.0</entry>
<entry>7.0</entry>
</row>
</tbody>
</tgroup>
</table>https://stackoverflow.com/questions/14756558
复制相似问题