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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:15+00:00 2026-05-25T23:39:15+00:00

I don’t think well in xsl, so I’m having trouble comprehending how to handle

  • 0

I don’t think well in xsl, so I’m having trouble comprehending how to handle this. I need to make an aggregate of the quantity, but grouped by the id in the attribute in the parent node. I’m thinking this should be a horribly simple recursion template, but I’m having trouble pumping it out

This input xml looks like this:

<foo>
  <lines>
    <line line_id="10000">
      <qty>10</qty>
    </line>
    <line line_id="10000">
      <qty>4</qty>
    </line>
    <line line_id="10000">
      <qty>12</qty>
    </line>
    <line line_id="20000">
      <qty>1</qty>
    </line>
    <line line_id="30000">
      <qty>4</qty>
    </line>
    <line line_id="30000">
      <qty>6</qty>
    </line>
  </lines>
  <lines>
    <line line_id="10000">
      <qty>4</qty>
    </line>
  </lines>
</foo>

The output should be:

<newfoo>
  <items>
    <item>
      <item_id>10000</item_id>
      <quantity>26</quantity>
    </item>
    <item>
      <item_id>20000</item_id>
      <quantity>1</quantity>
    </item>
    <item>
      <item_id>30000</item_id>
      <quantity>10</quantity>
    </item>
  </items>
  <items>
    <item>
      <item_id>10000</item_id>
      <quantity>4</quantity>
    </item>
  </items>
</newfoo>
  • 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-25T23:39:16+00:00Added an answer on May 25, 2026 at 11:39 pm

    This can be achieved in XSLT1.0 by using a method called Muenchian Grouping.

    Firstly, you define a key to look-up the line elements for a given lines element using the line_id attribute as the key. So, for a given lines element and line_id attribute, all matching line elements will be returned. You will need a concatenated key for this.

    <xsl:key name="id" 
       match="line" 
       use="concat(concat(generate-id(..), ','), @line_id)"/>
    

    Next, you need to match the line elements which have the first occurrence of each distinct line_id attribute within the lines element. This is achieved as follows:

    <xsl:apply-templates 
      select="line[
        generate-id() = 
        generate-id(key('id',concat(concat(generate-id(..),','),@line_id))[1])]" />
    

    i.e. Match line elements which happen to be the first elements in the list for the given key.

    Then, you can get the total quantity, simply by summing all elements in the key

    <xsl:value-of 
      select="sum(key('id', concat(concat(generate-id(..), ','), @line_id))/qty)"/>
    

    Here is the full XSLT document:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:key name="id" match="line" use="concat(concat(generate-id(..), ','), @line_id)"/>
    
       <xsl:template match="/foo">
          <newfoo>
             <xsl:apply-templates select="lines"/>
          </newfoo>
       </xsl:template>
    
       <xsl:template match="lines">
          <items>
             <xsl:apply-templates select="line[generate-id() = generate-id(key('id', concat(concat(generate-id(..), ','), @line_id))[1])]"/>
          </items>
       </xsl:template>
    
       <xsl:template match="line">
          <item>
             <item_id>
                <xsl:value-of select="@line_id"/>
             </item_id>
             <quantity>
                <xsl:value-of select="sum(key('id', concat(concat(generate-id(..), ','), @line_id))/qty)"/>
             </quantity>
          </item>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to the following XML:

    <foo>
       <lines>
          <line line_id="10000">
             <qty>10</qty>
          </line>
          <line line_id="10000">
             <qty>4</qty>
          </line>
          <line line_id="10000">
             <qty>12</qty>
          </line>
          <line line_id="20000">
             <qty>1</qty>
          </line>
          <line line_id="30000">
             <qty>4</qty>
          </line>
          <line line_id="30000">
             <qty>6</qty>
          </line>
       </lines>
       <lines>
          <line line_id="10000">
             <qty>4</qty>
          </line>
       </lines>
    </foo>
    

    The following XML is output:

    <newfoo>
       <items>
          <item>
             <item_id>10000</item_id>
             <quantity>26</quantity>
          </item>
          <item>
             <item_id>20000</item_id>
             <quantity>1</quantity>
          </item>
          <item>
             <item_id>30000</item_id>
             <quantity>10</quantity>
          </item>
       </items>
       <items>
          <item>
             <item_id>10000</item_id>
             <quantity>4</quantity>
          </item>
       </items>
    </newfoo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't ask me why but i can't use this method because I need to
Don't need to do this right now but thinking about the future... What would
Don't ask me why but I need to do the following: string ClassName =
I don't know why, but this code worked for me a month ago... maybe
Don't know if this is the right place to ask this, but I will
Don't think my virtualhost is working correctly. This is what I have inside of
Don't know why this is happening, but after submitting a form via JS (using
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if this is possible, but I have some code like this: val
Don't know if I'm over-thinking this or not.. but I'm trying to be able

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.