Jump to content

xslt sum and xpath


Recommended Posts

I am trying to sum the costs values in this sample xml using the xslt below.


Sample xml:

<?xml version="1.0"?><xml><object><objectid>183382</objectid><objectnumber>Test00001237</objectnumber><lccs><lcc><groupname>Costs: Acquisition planning</groupname><userfieldgroupid>9</userfieldgroupid><userfieldname>Concept development hours</userfieldname><userfieldid>42</userfieldid><fieldvalue>1.5</fieldvalue><depreciationlife>25</depreciationlife><costs>69</costs></lcc><lcc><groupname>Costs: Acquisition planning</groupname><userfieldgroupid>9</userfieldgroupid><userfieldname>Concept assessment hours</userfieldname><userfieldid>43</userfieldid><fieldvalue>0.5</fieldvalue><depreciationlife>25</depreciationlife><costs>23</costs></lcc></lccs></object></xml>

Sample xslt:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:js="urn:custom-javascriptscript" exclude-result-prefixes="msxsl js"><xsl:output method="html" omit-xml-declaration="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="yes"/><xsl:template match="/xml/object"><html><head></head><body>   <table>   <tr style="font-weight:bold" >   <td>Item</td>   <td>$</td>   </tr>   <xsl:for-each select="lccs/lcc">   <xsl:choose>   <xsl:when test="normalize-space(userfieldid)='42'">   <tr>   <td>   <xsl:value-of select="normalize-space(fieldvalue)"/></td>   <td> <xsl:value-of select="normalize-space(costs)"/></td>   </tr>   </xsl:when>   <xsl:when test="normalize-space(userfieldid)='43'">   <tr>   <td><xsl:value-of select="normalize-space(fieldvalue)"/></td>   <td><xsl:value-of select="normalize-space(costs)"/></td>   </tr>   </xsl:when>   </xsl:choose>   </xsl:for-each>   </table>   <table>   <tr>   <td>Subtotal</td>   <xsl:for-each select="lccs/lcc">   <xsl:choose>    <xsl:when test="userfieldgroupid='9'">   <xsl:if test ="costs !=''"> <td><xsl:value-of select="sum(../../*/*/costs)" /></td>   </xsl:if>   </xsl:when>   </xsl:choose>   </xsl:for-each>    </tr>   </table>    </body>   </html>   </xsl:template>   </xsl:stylesheet>

I am using an IE control in a database application to render a data view within the application. The underlying data source is SQL and the costs data type is decimal.

I expect to see a subtotal of 92, ie the sum of all costs in the userfieldgroupid = 9. Instead I get:Result -Item $1.5 690.5 23Subtotal 69 23I suspect the issues is with the xpath and that my result is the individual costs summed with themselves. If I use <xsl:value-of select="sum(../../*/*/costs)" />I get:Result -Item $1.5 690.5 23Subtotal NaN NaN


If I just use <xsl:value-of select='format-number(costs, "###,###.00")' /> directly on costs I get the numbers, but not summed! so back to where I started.



Item $

1.5 69

0.5 23

Subtotal 69.00 23.00

I have searched and tried so many examples but can't work this out so I would be very grateful for any help you can give me.


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...