列元素在输出中出现两次

Column elements are coming twice in the output

我有 table 元素,其中 tgroup 内的 colspec 作为输入,我想为 [=13] 包含 cols =] 元素。在尝试使用我的 XSL 代码时,它出现了两次

XML 我的输入:

<table colsep="1" rowsep="1" frame="all">
<tgroup>
    <colspec colwidth="100pt"/>
    <colspec colwidth="100pt"/>
    <thead>
        <row>
            <entry>Claims</entry>
            <entry>Claims</entry>
        </row>
    </thead>
    <tbody>
        <row>
            <entry>Claims</entry>
            <entry>Claims</entry>
        </row>
    </tbody>
</tgroup>

XSL 我已经尝试了 cols 元素:

<xsl:template match="table">
    <table>
      <xsl:attribute name="colsep"><xsl:value-of select="@colsep"/></xsl:attribute>
      <xsl:attribute name="frame"><xsl:value-of select="@frame"/></xsl:attribute>
      <xsl:attribute name="rowsep"><xsl:value-of select="@rowsep"/></xsl:attribute>
      <cols>
        <xsl:apply-templates select="tgroup/colspec"/>
      </cols>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="tgroup">
      <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="colspec">
    <col><xsl:attribute name="colwidth"><xsl:value-of select="@colwidth"/></xsl:attribute>
    <xsl:apply-templates/>
    </col>
  </xsl:template>
  
  <xsl:template match="thead">
    <thead>
      <xsl:apply-templates/>
    </thead>
  </xsl:template>
  
  <xsl:template match="tbody">
    <tbody>
      <xsl:apply-templates/>
    </tbody>
  </xsl:template>
  
  <xsl:template match="row">
    <tr>
      <xsl:apply-templates/>
    </tr>
  </xsl:template>
  
  <xsl:template match="entry">
    <td>
      <xsl:apply-templates/>
    </td>
  </xsl:template>

预期输出:

<table colsep="1" frame="all" rowsep="1">
    <cols>
        <col colwidth="100pt"/>
        <col colwidth="100pt"/>
    </cols>
    <thead>
        <tr>
            <td>Claims</td>
            <td>Claims</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Claims</td>
            <td>Claims</td>
        </tr>
    </tbody>
</table>

需要删除没有 cols 父元素的 colspec 并且需要关闭 cols.

中的 colspec 元素

稍微不同的方法怎么样?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tgroup">
    <cols>
       <xsl:apply-templates select="colspec"/>
    </cols>
    <xsl:apply-templates select="thead | tbody"/>
</xsl:template>

<xsl:template match="colspec">
    <col colwidth="{@colwidth}"/>
</xsl:template>

<xsl:template match="row">
    <tr>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="entry">
    <td>
        <xsl:apply-templates/>
    </td>
</xsl:template>
  
</xsl:stylesheet>