xslt 2.0: agrega consecutivamente valores de nodos anteriores

lets say I have the following source:

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

Now, I want to add to the initialAmount los nodos Amortization consecutively, so my output looks something like this:

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

How can I do this in xslt 2.0?

¡Muchas gracias!

preguntado el 28 de mayo de 14 a las 12:05

1 Respuestas

Utilice la herramienta

<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>

contestado el 28 de mayo de 14 a las 15:05

Thanks for your answer. But it is not completely what I need. Your solution adds the value of each node to the initial amount, but I want it to be added to the previous sum. - willi fischer

@user2986852, I have changed the code to address your problem. - Martín Honnen

Thanks, it works! It is beyond my understanding how it does it but I will try to figure it out. Thanks again. - willi fischer

Note there is a small error in it: </xsl:with-param> should be </xsl:apply-templates>, but I have no rights to edit your posting. - willi fischer

Sorry about the incorrect closing tag, now corrected. As for how it works, we start with the first Amortization y luego usar el following-sibling axis to step by step compute the values you are looking for, passing on the current value as a parameter. - Martín Honnen

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.