Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2013, 04:46 PM
Authorized User
 
Join Date: Jun 2013
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Default Sum Function Problem

I'm Jake, newbie to xml/xslt and this forum...thanks to all who give their time to help others. I've read much and viewed many tutorials in an effort to solve my own problems, but 1 eludes me. Running Win7, Net 2.0 (I think)...my apps(MS Flight Simulator and SimVar) auto generate the xml, whose format matches nothing I've seen on the Web:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='MissionProfile.xsl'?>
<MissionProfile>
<Profile name="SIMVAR_VERSION" value="V1.0.3537.0"/>
<Gather>
<Stat ID="" Name="GPS WP CROSS TRK" Unit="decimile">
<Point Time="4092177244">1.30295</Point>
<Point Time="4092177254">-2.30295</Point>
<Point Time="4092177264">3.30295</Point>
<Point Time="4092177274">-4.30295</Point></Stat></Gather>
<State/>
<Checksum>F6C0FB45AB5969A20043022EE9FFB8D4</Checksum></MissionProfile>

The Point nodes are created automatically by the apps as one flies the simulator. The @Time is a time stamp for the Point value, which is a measure, right (-) or left(+), of the planned flightpath.

I wish to average ALL Points, but since some are -, I can't simply combine and then divide by the count (or even better get avg function to work). I must first sum all negative values, multiply sum by -1, add back to the sum of the positive values and then divide by the count.

Here's what works (code lists all values of Point):
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:template match="/MissionProfile">
<html>
<h2>Here's your average Flight Plan deviation in decimiles</h2>
<body>
<xsl:for-each select="Gather/Stat/Point">
<xsl:value-of select="."/><xsl:text>, </xsl:text>
</xsl:for-each>
<br/>

This works (Sums ALL values of Point):
<xsl:for-each select="Gather/Stat">
<xsl:text>SumOfAll = </xsl:text><xsl:value-of select="sum(Point)" />
</xsl:for-each>
<br/>

This too works (Lists all values of Point that are -):
<xsl:text>ListOfAllLT0 = </xsl:text>
<xsl:for-each select="Gather/Stat/Point">
<xsl:if test=".&lt;0">
<xsl:value-of select="."/><xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<br/>

Here's my problem (Summing all those - values of Point)...no error, just blank:
<xsl:for-each select="Gather/Stat">
<xsl:text>SumOfAllLT0 = </xsl:text>
<xsl:if test=".&lt;0">
<xsl:value-of select="sum(Point)" />
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Thanks much for any assistance...
Jake
 
Old June 19th, 2013, 07:00 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Did you know that XSLT 2.0 is available on .NET? In fact there are two processors to choose from, Saxon and XmlPrime. This kind of problem becomes so much easier with 2.0 - you really don't want to be using the Microsoft processor, which ceased development years ago.

However, this one isn't too difficult in 1.0. It's just
Code:
(sum(Point[.>0]) - sum(Point[0>.])) div count(Point)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 19th, 2013, 09:17 PM
Authorized User
 
Join Date: Jun 2013
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Default Thank You, This is awesome...WROX rocks!

Just when I thought I had exhausted every possible combination of Templates, Keys, Variables, Parameters, If's...I'm sure I omitted some, you come up with the answer with one simple line of code. I became nauseated at many of the proposed solutions that exist out on the web. Intuitively, I thought surely it could not require all that! I can't thank you enough and only wish I had come to WROX FIRST.
Jake
The Following User Says Thank You to JakeRogers For This Useful Post:
jminatel (June 20th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with sum function patrickero XSLT 11 March 16th, 2010 03:50 AM
Sum function accuracy problem nuthib Crystal Reports 1 June 21st, 2004 02:12 AM
sum function problem sherr8 Access 1 February 13th, 2004 03:06 PM
SUM Function jmss66 Classic ASP Basics 17 July 29th, 2003 08:00 AM
Need Help with the Sum Function athanatos XSLT 1 July 22nd, 2003 10:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.