从 calstable 模型中去除空列 [英] Strip empty columns from calstable model

查看:30
本文介绍了从 calstable 模型中去除空列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆使用 CALS 模型标记的 XML 表,其中一些有我想删除的空列.所以这里是一些示例标记

I have a bunch of XML tables marked up using the CALS model and some of them have empty columns that I'd like to strip out. So here would be some example markup

              <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 列.在许多(大多数?)情况下,它将是最后一列,但并非总是如此.

So it's column 4 in the above example that I would like to delete completely. In many (most?) cases it will be the last column, but it wont always be.

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

And you will note that column 4 does contain spaces, or possibly &# 160; characters.

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

So how would I go about removing such entire columns using xslt ?

TIA

恐惧

推荐答案

请试一试:

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

除了删除空白列之外,它还负责对保留的列重新编号(我假设您会想要这个),因此对于这个输入,第二列是空白的:

In addition to removing blank columns, it also takes care of renumbering the columns that are kept (I assumed you would want this), so with this input, where the second column is blank:

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

这篇关于从 calstable 模型中去除空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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