问题描述
当namest 时,我正在尝试计算总和 tgroup/colspec/@colwidth 以 nameend 出现在 entry 元素中。
我运行了下面的 XSLT 代码,但没有出现预期的输出,请 帮助我 提前致谢!
输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<tgroup cols="9">
<colspec colwidth="60*"/>
<colspec colwidth="1*"/>
<colspec colwidth="1*"/>
<colspec colwidth="12*"/>
<colspec colwidth="1*"/>
<colspec colwidth="1*"/>
<colspec colwidth="1*"/>
<colspec colwidth="12*"/>
<colspec colwidth="1*"/>
</tgroup>
<thead>
<row>
<entry colname="col1"> </entry>
<entry colname="col2"/>
<entry colname="col3" namest="col3" nameend="col8">
<b>My content here</b>
</entry>
<entry colname="col9"/>
</row>
</thead>
</table>
我的 XSLT 代码
<xsl:output method="xml"/>
<xsl:template match="table">
<table width="90%">
<xsl:if test="tgroup/thead">
<thead>
<xsl:for-each select="tgroup/thead/row">
<tr>
<xsl:for-each select="entry">
<xsl:variable name="entrycol" select="@colname"/>
<td>
<xsl:if test="(@namest ne '') and (@nameend ne '')">
<xsl:attribute name="colspan">
<xsl:value-of select="number(substring-after(@nameend,'col')) - number(substring-after(@namest,'col')) + 1"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="style">
<xsl:if test="@colname = ancestor::tgroup/colspec/@colname">
<xsl:variable name="kk1" select="format-number(sum(ancestor::tgroup/colspec/@colwidth/xs:decimal(translate(.,'*',''))),'#.##')"/>
<xsl:text>width:</xsl:text>
<xsl:value-of select="concat(format-number(ancestor::tgroup/colspec[@colname = $entrycol][1]/number(translate(@colwidth,'')) div number($kk1) * 100,'#.##'),'%')"/>
<xsl:text>; </xsl:text>
</xsl:if>
</xsl:attribute>
<xsl:if test="tgroup/tbody">
<tbody>
<tr></tr>
</tbody>
</xsl:if>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</thead>
</xsl:if>
</table>
</xsl:template>
</xsl:stylesheet>
预期产出
<?xml version="1.0" encoding="UTF-8"?>
<table width="90%">
<thead>
<tr>
<td style="width:60%;">
<p> </p>
</td>
<td style="width:1%;">
<p></p>
</td>
<td colspan="6" style="width:28%;">
<p>
<b>My content here</b>
</p>
</td>
<td style="width:1%;">
<p></p>
</td>
</tr>
</thead>
<tbody>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
<tr> </tr>
</tbody>
</table>
解决方法
我不清楚那种格式,但也许
<xsl:template match="entry[@namest]">
<xsl:variable name="col-range" select="@namest/xs:integer(replace(.,'col','')) to @nameend/xs:integer(replace(.,''))"/>
<tr colspan="{$col-range[last()] - $col-range[1] + 1}" width="{sum(ancestor::tgroup/colspec[position() = $col-range]/@colwidth/xs:decimal(translate(.,'*','')))}%">
<xsl:apply-templates/>
</tr>
</xsl:template>
帮助。