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 471141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:57:21+00:00 2026-05-12T23:57:21+00:00

This is a sample of my XML, it can possibly have thousands of rows

  • 0

This is a sample of my XML, it can possibly have thousands of rows of items in a range of categories.

<store>
  <products type="computer">
    <item desc="text" amount="99"></c>
    <item desc="text" amount="69.95"></c>
    <item desc="text" amount="4.50"></c>
    <item desc="text" amount="10"></c>
    <item desc="text" amount="9.99"></c>
    <item desc="text" amount="24"></c>
  </products>
  <products type="books">
    <item desc="text" amount="5"></c>
    <item desc="text" amount="9.99"></c>
    <item desc="text" amount="24"></c>
  </products>      
  <products type="music">
    <item desc="text" amount="5"></c>
    <item desc="text" amount="1"></c>
    <item desc="text" amount="4.50"></c>
    <item desc="text" amount="10"></c>
    <item desc="text" amount="9.99"></c>
  </products>
</store>

What I want is some sort of loop that will allow me to create a header for every 100 items regardless of the product type. That’s easy enough but in this header I want a sum of the amounts of just the 100 items directly under that header.

Currently I have tried to slpit the tree into groups of 100 so I can perform a sum function on every group. I have been a few days at this problem but I fail to produce a complete working solution.

My output should look something like this (if the n was 3):

Header Total=173.45
text,99
text,69.95
text,4.50
Header Total=43.99
text,10
text,9.99
text,24
Header Total=38.99
text,5
text,9.99
text,24
Header Total=10.50
text,5
text,1
text,4.50
Header Total=19.99
text,10
text,9.99
  • 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-05-12T23:57:22+00:00Added an answer on May 12, 2026 at 11:57 pm

    Probably not the most efficient approach, but here’s one way:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8"/>
    
    <xsl:variable name="newline" select="'
    '" />
    
    <xsl:variable name="items" select="//item" />
    <xsl:variable name="items-count" select="count($items)" />
    <xsl:variable name="loop-size" select="3" />
    
    <xsl:template match="/">
        <xsl:call-template name="group-sum" />
    </xsl:template>
    
    <xsl:template name="group-sum">
        <xsl:param name="position" select="1" />
        <xsl:param name="sum" select="0" />
        <xsl:param name="group" select="''" />
    
        <xsl:variable name="current" select="$items[$position]" />
    
        <xsl:if test="$position != 1 and 
            (($position - 1) mod $loop-size = 0 or $position = $items-count + 1)">
            <xsl:value-of select="concat('Header Total=', $sum, $newline, $group)" />
        </xsl:if>
    
        <xsl:choose>
            <xsl:when test="$position != 1 and ($position - 1) mod $loop-size = 0">
                <!-- Start a new group -->
                <xsl:call-template name="group-sum">
                    <xsl:with-param name="position" select="$position + 1" />
                    <xsl:with-param name="sum" select="$current/@amount" />
                    <xsl:with-param name="group" select="concat($current/@desc, ',', $current/@amount, $newline)" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$position &lt;= $items-count">
                <!-- Append to the current group -->
                <xsl:call-template name="group-sum">
                    <xsl:with-param name="position" select="$position + 1" />
                    <xsl:with-param name="sum" select="$sum + $current/@amount" />
                    <xsl:with-param name="group" select="concat($group, $current/@desc, ',', $current/@amount, $newline)" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <!-- Finished -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
    

    Cheers
    Carlos

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems I can't easily have an XSD declaration for this simple XML <root>
I have a simple problem with XML got from database. Now, this XML can
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
Coding Platform: ASP.NET C# I have an XML like this. <Items> <Map id=35> <Terrains>
This is the sample xml document : <bookstore> <book category=COOKING> <title lang=english>Everyday Italian</title> <author>Giada
This question's solution makes hard-coded transformation of a sample XML tree into a flat
using LINQ to XML, this is a sample of my XML <shows> <Show Code=456
I have the following XML <ns0:Root xmlns:ns0=http://Map_Sample.Input_Schema> <Number1> </Number1> <Number2>11</Number2> <Number3>12</Number3> </ns0:Root> In this
I have this simple XML file : <?xml version=1.0 encoding=utf-8 ?> <Artists> <artist artistId=1>
XML's can be transformed by a browser using XSL. This can be either done

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.