Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9304231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:39:50+00:00 2026-06-18T23:39:50+00:00

Got my head stuck on some simple XSL functions. I need to return the

  • 0

Got my head stuck on some simple XSL functions. I need to return the total and average sale per product and also total sales per day from this XML.

<products>
    <product>
        <id>1</id>
        <brand>Samsung</brand>  
        <model>Galaxy</model>
        <serialno>00345234</serialno>
        <sales>
            <sale day="20130211">1200</sale>
            <sale day="20130212">9000</sale>
            <sale day="20130213">5400</sale>
        </sales>
    </product>
    <product>
        <id>2</id>
        <brand>Sony</brand> 
        <model>Bravia</model>
        <serialno>07432200</serialno>
        <sales>
            <sale day="20130211">3400</sale>
            <sale day="20130212">990</sale>
            <sale day="20130213">1400</sale>
        </sale>
    </product>
</products>

Output

Total sales per day: 21390
Total (S/N 00345234): 15600
Average (S/N 00345234): 5200
Total (S/N 07432200): 5790
Average (S/N 07432200): 1930
Total sales for day 20130212 = 9990
Total sales for day 20130213 = 6800
Total sales for day 20130211 = 4600
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:39:51+00:00Added an answer on June 18, 2026 at 11:39 pm

    In short, the answer is using the sum() function with XSL keys and the <xsl:sort> element. This XSLT 1.0 compatible example might help you get started:

    Stylesheet:

    <?xml version="1.0" encoding="iso-8859-1"?>
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
    
      <!-- Index <sale> elements using the @day attribute -->
      <xsl:key name="kSale" match="sale" use="@day"/>
    
      <xsl:template match="product">
        <xsl:variable name="serialNo" select="serialno"/>
        <!-- Add together all sales for today. -->
        <xsl:variable name="totalSales" select="sum(sales/sale)"/>
        <!-- Divide total sales by the amount of sales. -->
        <xsl:variable name="averageSales" select="$totalSales div count(sales/sale)"/>
        Total (S/N <xsl:value-of select="$serialNo"/>): <xsl:value-of select="$totalSales"/>
        Average (S/N <xsl:value-of select="$serialNo"/>): <xsl:value-of select="$averageSales"/>
      </xsl:template>
    
      <xsl:template match="sale">
        <!--
        Get the total sales for this day by adding together the values of all <sale>
        elements with the same @day attribute as this one.
        -->
        <xsl:variable name="totalSalesForDay" select="sum(key('kSale', @day))"/>
        Total sales for day <xsl:value-of select="@day"/>: <xsl:value-of select="$totalSalesForDay"/>
      </xsl:template>
    
      <xsl:template match="/">
        <xsl:apply-templates select="products/product"/>
        <xsl:apply-templates select="products/product[1]/sales/sale">
          <!-- Sort the sales per day in descending order. -->
          <xsl:sort select="." order="descending" data-type="number"/>
        </xsl:apply-templates>
      </xsl:template>
    </xsl:stylesheet>
    

    Output:

    Total (S/N 00345234): 15600
    Average (S/N 00345234): 5200
    Total (S/N 07432200): 5790
    Average (S/N 07432200): 1930
    Total sales for day 20130212: 9990
    Total sales for day 20130213: 6800
    Total sales for day 20130211: 4600
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble getting my head around the jquery queue method. I've got
I'm learning MongoDB and Casbah by writing a simple app. Got stuck when I
I've got one head scratcher of a problem that I can't seem to find
I have the 2 following LINQ queries and I haven't quite got my head
got a problem, can't get my head around this jquery and would appreciate your
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I've got the following situation: <html> <head> <title> Title </title> <style type=text/css> #wrapper {
I'll make this short, I got this webpage : <html xmlns=http://www.w3.org/1999/xhtml> <head> <link rel=Stylesheet
Got a programm with 2 threads. one thread is writing some stuff into the
I got stuck. I am trying to form a function that will eat classless

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.