Got my head stuck on some simple XSL functions. I need to return the total and average sale per product and also total sales per day from this XML.
<products>
<product>
<id>1</id>
<brand>Samsung</brand>
<model>Galaxy</model>
<serialno>00345234</serialno>
<sales>
<sale day="20130211">1200</sale>
<sale day="20130212">9000</sale>
<sale day="20130213">5400</sale>
</sales>
</product>
<product>
<id>2</id>
<brand>Sony</brand>
<model>Bravia</model>
<serialno>07432200</serialno>
<sales>
<sale day="20130211">3400</sale>
<sale day="20130212">990</sale>
<sale day="20130213">1400</sale>
</sale>
</product>
</products>
Output
Total sales per day: 21390
Total (S/N 00345234): 15600
Average (S/N 00345234): 5200
Total (S/N 07432200): 5790
Average (S/N 07432200): 1930
Total sales for day 20130212 = 9990
Total sales for day 20130213 = 6800
Total sales for day 20130211 = 4600
In short, the answer is using the
sum()function with XSL keys and the<xsl:sort>element. This XSLT 1.0 compatible example might help you get started:Stylesheet:
Output: