I am new to xsl transformations and I have a question. I am looping through an xml like this:
<PO>
<Items>
<Item>
<Price>2</Price>
<Quantity>5</Quantity>
</Item>
<Item>
<Price>3</Price>
<Quantity>2</Quantity>
</Item>
</Items>
<QuantityTotal></QuantityTotal>
</PO>
Now I want to insert a value in the QuantityTotal node:
The value is the sum of price*quantity of all items, in this case (2*5)+(3*2) = 16
How can I do this, I tried it with a loop and variables, but variables are immutable so I don't know how I can achieve this.
Thx for your help
Here is an XSLT solution -- no extension functions required:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="QuantityTotal">
<xsl:copy>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="../Items/Item"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="sumProducts">
<xsl:param name="pNodes"/>
<xsl:param name="pSum" select="0"/>
<xsl:param name="pEname1" select="'Price'"/>
<xsl:param name="pEname2" select="'Quantity'"/>
<xsl:choose>
<xsl:when test="not($pNodes)">
<xsl:value-of select="$pSum"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select=
"$pNodes[position() > 1]"/>
<xsl:with-param name="pSum" select=
"$pSum
+
$pNodes[1]/*[name()=$pEname1]
*
$pNodes[1]/*[name()=$pEname2]
"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<PO>
<Items>
<Item>
<Price>2</Price>
<Quantity>5</Quantity>
</Item>
<Item>
<Price>3</Price>
<Quantity>2</Quantity>
</Item>
</Items>
<QuantityTotal></QuantityTotal>
</PO>
the wanted result is produced:
<PO>
<Items>
<Item>
<Price>2</Price>
<Quantity>5</Quantity>
</Item>
<Item>
<Price>3</Price>
<Quantity>2</Quantity>
</Item>
</Items>
<QuantityTotal>16</QuantityTotal>
</PO>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With