Calculating a Sum of Sums (First Attempt)
The Initial Example for Sum of Sums, inv.xsl, does not generate the sum of all extended prices. If you needed this kind of functionality, you would have to enhance your style sheet. As a first attempt at a solution, you might try something like the following. This third <tr>
element is added to inv.xsl, resulting in inv2.xsl.
<tr> <th colspan="3">Invoice Total</th> <td align="right"> <xsl:value-of select="format-number(sum(item/quantity * item/unit_price), '#,##0.00')"/> </td> </tr>
The result of this approach is shown in the following example.
Example
XML File (invoice.xml)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="inv2.xsl" ?> <invoice> <item item_num="AX43598"> <quantity>29</quantity> <unit_price>2.00</unit_price> </item> <item item_num="FH29782"> <quantity>140</quantity> <unit_price>6.50</unit_price> </item> </invoice>
XSLT File (inv2.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <table border="1" cellpadding="2" cellspacing="0"> <tr> <th>Item</th> <th>Quantity</th> <th>Unit Price</th> <th>Extended Price</th> </tr> <xsl:for-each select="invoice/item"> <tr> <td> <xsl:value-of select="@item_num"/> </td> <td align="right"> <xsl:value-of select="quantity"/></td> <td align="right"> <xsl:value-of select="format-number(unit_price, '#,##0.00')"/> </td> <td align="right"> <xsl:value-of select="format-number(quantity * unit_price, '#,##0.00')"/> </td> </tr> </xsl:for-each> <tr> <th colspan="3">Invoice Total</th> <td align="right"> <xsl:value-of select="format-number(sum(item/quantity * item/unit_price), '#,##0.00')"/> </td> </tr> </table> </xsl:template> </xsl:stylesheet>
Result
Internet Explorer (or any other compliant XSLT processor) treats this as an error. The following is the message from Internet Explorer:
Expression does not return a DOM node. format-number(-->sum(item/quantity * item/unit_price)<--, '#,##0.00')