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 March 30th, 2016, 10:14 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum multiple attributes of multiple elements

I'm kind of new to xslt and need help getting a grand total of all overtime hours. I can get each individuals total of overtime hours, but not a grand total. Any help is greatly appreciated.

Here is an XML sample:

Code:
<?xml version='1.0' encoding='UTF-8'?><dd:Report_Data xmlns:dd="urn:com.ptl.report/CR_Headcount">
<dd:Report_Entry><dd:Today>03292016 16:41</dd:Today>
        <dd:Employee_ID>12345</dd:Employee_ID>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>-10.5</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>4</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>10.25</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>12</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>15</dd:OT_Hours>
        </dd:Payroll_Result_Lines__All_>
    </dd:Report_Entry>
<dd:Report_Entry><dd:Today>03292016 16:41</dd:Today>
        <dd:Employee_ID>23456</dd:Employee_ID>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>8</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>9.5</dd:OT_Hours>
    </dd:Report_Entry>
<dd:Report_Entry><dd:Today>03292016 16:41</dd:Today>
        <dd:Employee_ID>34567</dd:Employee_ID>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>4</dd:OT_Hours>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>9.5</dd:OT_Hours>
        </dd:Payroll_Result_Lines__All_>
    </dd:Report_Entry>
<dd:Report_Entry><dd:Today>03292016 16:41</dd:Today>
        <dd:Employee_ID>45678</dd:Employee_ID>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>10</dd:OT_Hours>
            <dd:Amount>263.4</dd:Amount></dd:Payroll_Result_Lines__All_>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>15</dd:OT_Hours>
        </dd:Payroll_Result_Lines__All_>
    </dd:Report_Entry>
<dd:Report_Entry><dd:Today>03292016 16:41</dd:Today>
        <dd:Employee_ID>56789</dd:Employee_ID>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>16</dd:OT_Hours>
            <dd:Amount>433.2</dd:Amount></dd:Payroll_Result_Lines__All_>
        <dd:Payroll_Result_Lines__All_><dd:OT_Hours>16</dd:OT_Hours>
        </dd:Payroll_Result_Lines__All_>
    </dd:Report_Entry>
</dd:Report_Data>

Here is a sample of my xsl file. I am using Saxon PE 9.5.1.7


Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="no" method="text"/>
    <xsl:template match="dd:Report_Data" xmlns:dd="urn:com.ptl.report/CR_Headcount">
        <xsl:for-each select="dd:Report_Entry">
            <xsl:variable name="tmpTotal">
                <xsl:value-of select="sum(dd:Payroll_Result_Lines__All_/dd:OT_Hours)"/>
            </xsl:variable>
            <xsl:value-of select="sum($tmpTotal)"/>
            <xsl:call-template name="insertDelimiter"/>
        </xsl:for-each>
        <xsl:call-template name="insertNewLine"/>
        <xsl:value-of select="count(dd:Report_Entry)"/>
    </xsl:template>
    <!-- Starting function library section here -->
    <xsl:template name="insertDelimiter">
        <xsl:text>|</xsl:text>
    </xsl:template>
    <xsl:template name="insertNewLine">
        <!-- Used to insert a new line into the XML output -->
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

The output i get is:

30.75|17.5|13.5|25|32|
5

The output i would like is a sum of the first line in the output above:

118.25|
5
  #2 (permalink)  
Old March 30th, 2016, 10:29 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

Would sum(dd:Report_Entry/dd:Payroll_Result_Lines__All_/dd:OT_Hours) not do what you want, without the for-each loop?

Also, your XML above doesn't look valid (lack of closing tags for the Payroll_Result element).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #3 (permalink)  
Old March 30th, 2016, 10:46 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up That worked!

Thank you very very much for the quick response 'samjudson'. Did not realize i did not need the for each. That did exactly what i needed. Sorry for the missing part of the xml file. I was trying to shorten it and must have left some things out.

I will be looking into purchasing some good reference books here.

Fantastic site!


Similar Threads
Thread Thread Starter Forum Replies Last Post
LDAP Attributes with multiple values Ron Howerton ASP.NET 4 General Discussion 0 March 31st, 2014 04:23 PM
Merge XML files with multiple attributes ashwini kulsange XSLT 6 May 29th, 2013 04:20 PM
Extracting multiple elements from multiple files Daedalus XSLT 2 October 10th, 2012 10:31 AM
Multiple SUM()-WHERE clauses Arbec Access 5 June 28th, 2011 10:27 AM
multiple self joins and sum juan@jbstech.net SQL Language 3 May 25th, 2004 06:03 PM





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