【发布时间】:2020-04-25 07:42:57
【问题描述】:
当namest 与tgroup/colspec/@colname 中的nameend 值匹配时,我正在尝试计算总和tgroup/colspec/@colwidth。我只计算了 colname="COLSPEC0" 和 colname="1" 中的属性 colwidth。 colname="COLSPEC1" 中缺少 colwidth。
Input.XML
<?xml version="1.0" encoding="UTF-8"?>
<table frame="all" orient="portrait">
<title>MEDICARE</title>
<tgroup align="center" cols="4">
<colspec colname="COLSPEC0" colwidth="6.86pi"/>
<colspec colname="COLSPEC1" colwidth="6.31pi"/>
<colspec colname="1" colwidth="5.75pi"/>
<colspec colname="2" colwidth="5.87pi"/>
<tbody>
<row>
<entry nameend="1" namest="COLSPEC0">
<para flow="new">If beneficiary files</para>
</entry>
<entry align="center" colname="2" colsep="1" morerows="0" nameend="2" namest="2" rowsep="1">
<para flow="new">Monthly premium in 2019 is:</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
我的 XSLT 代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="entry">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="namest" select="@namest"/>
<xsl:variable name="nameend" select="@nameend"/>
<xsl:variable name="namestPos" select="number(substring-before(ancestor::tgroup/colspec[@colname=$namest]/@colwidth, 'pi'))"/>
<xsl:variable name="nameendPos" select="number(substring-before(ancestor::tgroup/colspec[@colname=$nameend]/@colwidth, 'pi'))"/>
<xsl:if test="@namest != @nameend">
<xsl:attribute name="width">
<xsl:value-of select="$namestPos + $nameendPos"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期输出
<?xml version="1.0" encoding="UTF-8"?><table frame="all" orient="portrait">
<title>MEDICARE</title>
<tgroup align="center" cols="4">
<colspec colname="COLSPEC0" colwidth="6.86pi"/>
<colspec colname="COLSPEC1" colwidth="6.31pi"/>
<colspec colname="1" colwidth="5.75pi"/>
<colspec colname="2" colwidth="5.87pi"/>
<tbody>
<row>
<entry nameend="1" namest="COLSPEC0" width="18.92">
<para flow="new">If beneficiary files</para>
</entry>
<entry align="center" colname="2" colsep="1" morerows="0" nameend="2" namest="2" rowsep="1">
<para flow="new">Monthly premium in 2019 is:</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
如果我运行此代码,则在 width="12.61" 中创建总和值。
【问题讨论】: