从calstable模型剥离空列

时间:2013-02-07 16:54:28

标签: xml xslt xslt-2.0

我有一堆使用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列我想完全删除。 在许多(大多数?)案例中,它将是最后一列,但它并不总是如此。

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

那么我将如何使用xslt删除整个列?

TIA

Feargal

1 个答案:

答案 0 :(得分: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>