You might have run into a requirement where you need to compute the total value of all items in a purchase order. Consider the PO XML shown below.
<?xml version="1.0" encoding="UTF-8" ?>
<po xmlns="http://www.example.org">
<Items>
<item>
<price>1</price>
<qty>2</qty>
</item>
<item>
<price>3</price>
<qty>4</qty>
</item>
<item>
<price>5</price>
<qty>6</qty>
</item>
</Items>
</po>
You might want to compute the total value of all items in the purchase Order. For computing this, you would need to multiply the price and qty for each item, and add them up. In XSLT 1.0, I guess the way to do this would be to recursively invoke a template that computes the sum total. In XSLT 2.0, this can be done in a very elegant single expression.
<xsl:variable name="itemVar" select="/tns:po/tns:Items/tns:item"/>
<xsl:template match="/">
<tns:poSummary>
<tns:totalValue><xsl:value-of select="sum(for $i in $itemVar return ($i/tns:price * $i/tns:qty))"/></tns:totalValue>
</tns:poSummary>
</xsl:template>
</xsl:stylesheet>
Comments (1)
good content...
Posted by Raghuram | August 11, 2008 5:56 AM
Posted on August 11, 2008 05:56