xslt 2.0:连续添加前面节点的值

时间:2014-05-28 11:55:07

标签: xml add xslt-2.0

假设我有以下来源:

<root>
    <initialAmount>1000</initialAmount>
    <Amortization_List>
        <Amortization Index="0">10</Amortization>
        <Amortization Index="1">25</Amortization>
        <Amortization Index="2">-10</Amortization>
    </Amortization_List>
</root>

现在,我想连续添加initialAmount节点Amortization,所以我的输出看起来像这样:

<result>
    <amount>1010</amount>
    <amount>1035</amount>
    <amount>1025</amount>
</result>

如何在xslt 2.0中执行此操作?

非常感谢!

1 个答案:

答案 0 :(得分:1)

使用

<xsl:template match="root">
  <result>
    <xsl:variable name="amount" select="initialAmount"/>
    <xsl:apply-templates select="Amortization_List/Amortization[1]">
      <xsl:with-param name="sum" select="$amount"/>
    </xsl:apply-templates>
  </result>
</xsl:template>

<xsl:template match="Amortization">
  <xsl:param name="sum"/>
  <xsl:variable name="amount" select=". + $sum"/>
  <amount><xsl:value-of select="$amount"/></amount>
  <xsl:apply-templates select="following-sibling::Amortization[1]">
    <xsl:with-param name="sum" select="$amount"/>
  </xsl:apply-templates>
</xsl:template>