xslt - How can I summarize nodes using calculation data from other nodes -
using xslt 1.0, how can summarize subnodes under given node while modifiyng content data set of nodes in elegant way? assume have xml:
<root> <exchangerates> <exchangerate> <currencycode>usd</currencycode> <rate>6.4</rate> </exchangerate> <exchangerate> <currencycode>eur</currencycode> <rate>8.44</rate> </exchangerate> <exchangerate> <currencycode>sek</currencycode> <rate>1</rate> </exchangerate> </exchangerates> <prices> <price> <currency>sek</currency> <amount>10000</amount> </price> <price> <currency>eur</currency> <amount>1000</amount> </price> <price> <currency>usd</currency> <amount>1000</amount> </price> </prices> </root>
i want sum of amounts converted sek of exchangerates. result should be:
<suminsek>24840</suminsek>
if didn't have convert amounts use xpath sum() function. is possible use function in case?
another possible solution without recursive calls exsl extension. make use of key definition form @softwarebear.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"> <xsl:key name="rates" match="//exchangerate/rate" use="parent::*/child::currencycode/text()"/> <xsl:template match="/" > <xsl:apply-templates select="//prices"/> </xsl:template> <xsl:template match="prices"> <suminsek> <xsl:variable name="price_sek"> <xsl:apply-templates mode="sek" /> </xsl:variable> <xsl:value-of select="sum(exsl:node-set($price_sek)//price_sek)"/> </suminsek> </xsl:template> <xsl:template match="price" mode="sek"> <price_sek> <xsl:value-of select="number(amount) * number( key('rates', currency) )" /> </price_sek> </xsl:template> </xsl:stylesheet>
wiht output
<suminsek>24840</suminsek>
Comments
Post a Comment