如何解决列元素在输出中出现两次
我正在使用table元素,该元素以colspec
内的tgroup
作为输入,我想为cols
元素包括colspec
。尝试使用我的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>
我尝试过cols元素的XSL:
<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>
需要删除没有colspec
父元素的cols
,并且需要关闭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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。