如何计算 colspec/@colwidth 属性值的总和匹配属性 namest 到 entry 元素中的 nameend-XSLT

How to Calculate Sum of colspec/@colwidth Attribute value in match Attribute namest to nameend in entry Element- XSLT

我正在尝试计算总和 tgroup/colspec/@colwidth,当 namest 到 nameend 值在 tgroup/colspec/@colname 中匹配时。我只计算了 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" 中创建总和值。

我会这样做:

  <xsl:template match="entry[@namest != @nameend]">
      <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:attribute 
            name="width" 
            select="let $colspecs := ancestor::tgroup/colspec,
                    $names := data($colspecs/@colname)
                    return sum($colspecs[position() = index-of($names, current()/@namest) to index-of($names, current()/@nameend)]/@colwidth/xs:decimal(substring-before(., 'pi')))"/>
          <xsl:apply-templates/>                                    
      </xsl:copy>
  </xsl:template>

https://xsltfiddle.liberty-development.net/bEzknsT

如果您只有 XSLT 2 支持,那么您需要为 colspecsnames 使用两个 xsl:variable 声明,而不是我使用的内联 XPath let