【问题标题】:How to Calculate Sum of colspec/@colwidth Attribute value in match Attribute namest to nameend in entry Element- XSLT如何计算 colspec/@colwidth 属性值的总和在条目 Element-XSLT 中匹配属性 namest 到 nameend
【发布时间】: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" 中创建总和值。

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    我会这样做:

      <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

    【讨论】:

    • 谢谢@Martin Honnen,先生!
    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多