It is not clear what the question is, but I guess that is.
This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:param name="pPriceName" select="'price2'"/> <xsl:param name="pQuantityName" select="'quantity1'"/> <xsl:template match="/"> <xsl:call-template name="totalSales"> <xsl:with-param name="pSales" select="/*/*"/> <xsl:with-param name="pPriceName" select="$pPriceName"/> <xsl:with-param name="pQuantityName" select="$pQuantityName"/> </xsl:call-template> </xsl:template> <xsl:template name="totalSales"> <xsl:param name="pAccum" select="0"/> <xsl:param name="pSales"/> <xsl:param name="pPriceName"/> <xsl:param name="pQuantityName"/> <xsl:variable name="vthisSale" select="$pSales[1]"/> <xsl:choose> <xsl:when test="not($vthisSale)"> <xsl:value-of select="$pAccum"/> </xsl:when> <xsl:otherwise> <xsl:call-template name="totalSales"> <xsl:with-param name="pAccum" select= "$pAccum + $vthisSale/*[name()=$pPriceName] * $vthisSale/*[name()=$pQuantityName] "/> <xsl:with-param name="pSales" select= "$pSales[position() >1]"/> <xsl:with-param name="pPriceName" select="$pPriceName"/> <xsl:with-param name="pQuantityName" select="$pQuantityName"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
when applied to the following XML document (you could at least provide this!):
<orders> <order> <price1>10</price1> <quantity1>3</quantity1> <price2>15</price2> <quantity2>1</quantity2> </order> <order> <price1>11</price1> <quantity1>2</quantity1> <price2>9</price2> <quantity2>3</quantity2> </order> </orders>
creates the desired, correct result:
63
Pay attention . The values โโof the names of the children, which will serve as the price and quantity, are provided as external parameters for conversion and can only be known at run time.
source share