2013-02-07 30 views
0

我有一堆使用CALS模型标记的XML表,并且其中一些列有我想要去掉的空列。因此,这里是一些示例标记从Calstable模型中去掉空列

   <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列,我想完全删除。 在许多(大多数?)情况下,它将是最后一列,但它不会总是。

你会注意到,第4列确实包含空格,或可能是&#160;字符。

那么我会如何去除使用xslt这样的整个列?

TIA

Feargal

+0

您会在一个文档中处理多个表格的XML文档吗?我已经发布了一个可能的解决方案,但我认为它不能用于具有多个表的XML。 – JLRishe

回答

1

请这给一试:

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

看起来它会做的伎俩。你能解释一下“count(。| preceding-sibling :: entry)”表达式吗? – Feargal