Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old October 24th, 2007, 12:39 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to sum

Following is the xml file sample

<Records>
  <Activity>
    <ServiceType>Desktop Risk Assessment</ServiceType>
    <TotalHours>9</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>732.5</TotalCost>
  </Activity>
  <Activity>
    <ServiceType>Engineering Follow Up</ServiceType>
    <TotalHours>4</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>400</TotalCost>
  </Activity>
  <Activity>
    <ServiceType>Engineering Follow Up</ServiceType>
    <LocationCount>1</LocationCount>
    <VisitCount>2</VisitCount>
    <TotalHours>8</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>832.5</TotalCost>
  </Activity>
<Activity>
    <ServiceType>Engineering Follow Up</ServiceType>
    <LocationCount>1</LocationCount>
    <VisitCount>7</VisitCount>
    <TotalHours>9</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>732.5</TotalCost>
  </Activity>
<Activity>
    <ServiceType>Engineering Follow Up</ServiceType>
    <LocationCount>1</LocationCount>
    <VisitCount>3</VisitCount>
    <TotalHours>5</TotalHours>
    <TotalExpenses>20</TotalExpenses>
    <TotalCost>632.5</TotalCost>
  </Activity>
  <Activity>
    <ServiceType>First Jurisdictional</ServiceType>
    <TotalHours>9</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>732.5</TotalCost>
  </Activity>
</Records>

Based on the ServiceType I have to sum up 'TotalHours'

for example we have 'Engineering Follow Up' based on this we have to sum 'TotalHours' , TotalCost etc in xslt 1.0 and it cannot be hard coded 'Engineering Follow Up'

OUTPUT
Desktop Risk Assessment

<TotalHours>9</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>732.5</TotalCost>
Engineering Follow Up
<TotalHours>26</TotalHours>
<TotalCost>3666.77</TotalCost>

First Jurisdictional
<TotalHours>9</TotalHours>
    <TotalExpenses>0</TotalExpenses>
    <TotalCost>732.5</TotalCost>


Please find the xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:exslt="http://exslt.org/common" xmlns:date="http://exslt.org/dates-and-times">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


<xsl:template match="Records">
    <xsl:apply-templates select="Activity">
     <xsl:sort select="ServiceType" order="ascending" data-type="number" />
    </xsl:apply-templates>
</xsl:template>


    <xsl:template match="Activity">
    <xsl:choose>
    
     <xsl:when test="(ServiceType = 'Engineering Follow Up')">

         <xsl:variable name="EngFollowUpTotalHrs" select="sum(//Activity[ServiceType='Engineering Follow Up']/TotalHours)"/>
         <xsl:value-of select="$EngFollowUpTotalHrs" />
         <xsl:variable name="EngFollowUpTotalExp" select="sum(//Activity[ServiceType='Engineering Follow Up']/TotalExpenses)"/>
         <xsl:value-of select="$EngFollowUpTotalExp" />
         <xsl:variable name="EngFollowUpTotalCost" select="sum(//Activity[ServiceType='Engineering Follow Up']/TotalCost)"/>
         <xsl:value-of select="$EngFollowUpTotalCost" />

     </xsl:when>
     <xsl:otherwise>
        <xsl:value-of select="ServiceType" />

     </xsl:otherwise>
    </xsl:choose >

</xsl:template>

</xsl:stylesheet>
  #2 (permalink)  
Old October 24th, 2007, 12:57 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using xslt 1.0.

I tried with for each loop , but First time it will enter in to the
Engg Follow up,it is summming up under this type ,second time it is again going in to the loop. I have to prevent to enter in to second time, If condtion or can we set Flag. Please suggest me

Thanks
  #3 (permalink)  
Old October 24th, 2007, 02:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Look at the first post in the XSLT forum. There is a link to grouping in version 1.0. You need to group on ServiceType using a key, then you can sum.

--

Joe (Microsoft MVP - XML)
  #4 (permalink)  
Old October 24th, 2007, 03:20 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joefawcett,

I am new to this key concept , Can you modify this xslt

Thanks
satish
  #5 (permalink)  
Old October 24th, 2007, 03:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

No, the forums designed for help, if you want me to do the work for you then we can talk about a contract.

--

Joe (Microsoft MVP - XML)
  #6 (permalink)  
Old October 24th, 2007, 03:32 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You appear to be doing the following as well:

<xsl:sort select="ServiceType" order="ascending" data-type="number" />

ServiceType isn't a number.

/- Sam Judson : Wrox Technical Editor -/
  #7 (permalink)  
Old October 24th, 2007, 03:43 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't shown the output you want, but if you want a total across activities, then I wouldn't expect to be computing it while processing an individual activity. I would expect to see something like

<xsl:template match="Records">
  <total activity="{$param}">
    <xsl:value-of select="sum(Activity[ServiceType=$param]/TotalHours"/>
  </total>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sum in for-each Gjella XSLT 1 October 2nd, 2006 08:48 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
sum utarian Access 2 March 28th, 2005 09:42 PM
need sum help! jbik BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 December 20th, 2004 05:27 PM
SUM or What bertlf Pro VB Databases 2 November 29th, 2003 02:44 PM





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