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

  • Home
  • SEARCH
  • 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 6383291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:37:52+00:00 2026-05-25T02:37:52+00:00

Hi I want to do some aggregation within the XSLT. For example I have

  • 0

Hi I want to do some aggregation within the XSLT. For example I have the following xml file.

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<reporting:root xmlns:reporting="testing">
  <reporting:default0 reporting:type="Portfolio">
    <reporting:window reporting:Id="1" reporting:level="0" reporting:name="TEST" reporting:parentId="-1">
      <reporting:folio reporting:Id="2" reporting:criteria="0" reporting:level="1" reporting:name="topfolder1" reporting:parentId="1">
        <reporting:folio reporting:Id="37" reporting:criteria="0" reporting:level="2" reporting:name="folder2" reporting:parentId="2">
          <reporting:folio reporting:Id="38" reporting:criteria="0" reporting:level="3" reporting:name="folder3" reporting:parentId="37">
            <reporting:folio reporting:Id="196" reporting:criteria="0" reporting:level="4" reporting:name="folder4" reporting:parentId="38">
              <reporting:line reporting:Id="123456" reporting:level="5" reporting:name="element1" reporting:parentId="196" reporting:positionType="0">
                <reporting:reference>element1</reporting:reference>
                <reporting:number>625</reporting:number>
              </reporting:line>
              <reporting:line reporting:Id="223456" reporting:level="5" reporting:name="element2" reporting:parentId="196" reporting:positionType="7">
                <reporting:reference>element2</reporting:reference>
                <reporting:number>475</reporting:number>
              </reporting:line>
              <reporting:folio reporting:Id="209" reporting:criteria="0" reporting:level="5" reporting:name="delta" reporting:parentId="196">
                <reporting:line reporting:Id="223456" reporting:level="6" reporting:name="element2" reporting:parentId="209" reporting:positionType="0">
                  <reporting:reference>element2</reporting:reference>
                  <reporting:number>190</reporting:number>
                </reporting:line>
              </reporting:folio>
            </reporting:folio>
          </reporting:folio>
        </reporting:folio>
      </reporting:folio>
      <reporting:folio reporting:Id="4" reporting:criteria="0" reporting:level="1" reporting:name="topfolder2" reporting:parentId="1">
        <reporting:folio reporting:Id="39" reporting:criteria="0" reporting:level="2" reporting:name="folder24" reporting:parentId="4">
          <reporting:folio reporting:Id="40" reporting:criteria="0" reporting:level="3" reporting:name="folder34" reporting:parentId="39">
            <reporting:folio reporting:Id="296" reporting:criteria="0" reporting:level="4" reporting:name="folder44" reporting:parentId="40">
              <reporting:line reporting:Id="123456" reporting:level="5" reporting:name="element3" reporting:parentId="296" reporting:positionType="0">
                <reporting:reference>element3</reporting:reference>
                <reporting:number>65525</reporting:number>
              </reporting:line>
              <reporting:folio reporting:Id="309" reporting:criteria="0" reporting:level="5" reporting:name="delta" reporting:parentId="296">
                <reporting:line reporting:Id="2234567" reporting:level="6" reporting:name="element4" reporting:parentId="309" reporting:positionType="0">
                  <reporting:reference>element4</reporting:reference>
                  <reporting:number>490</reporting:number>
                </reporting:line>
              </reporting:folio>
            </reporting:folio>
          </reporting:folio>
        </reporting:folio>
      </reporting:folio>
    </reporting:window>
  </reporting:default0>
</reporting:root> 

then I want to do some aggregation on ‘level 2’. i.e anything inside ‘reporting:line’ should be returned and it’s level 1 and level 2 parent ‘reporting:folio’ should also be returned. Also for the same element under anysubfolder of level2 folder should be aggregated as one. and the sum(number) is also calculated.

Also the no. of sub folios can be 200 before there is a reporting:line tag.

so for this xml I want the result to be:

topfolder,folder2,element1,625
topfolder,folder2,element2,665
topfolder2,folder24,element3,65525
topfolder2,folder24,element4,490

Hope I explained it correctly. Really appreciate your help.

  • 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-25T02:37:53+00:00Added an answer on May 25, 2026 at 2:37 am

    In this case, you need to do grouping first and then aggregation. You need to group elements by folio level 1, folio level 2, and element name. To do this, you usually use a method of Muenchian grouping.

    Firstly, you define a xsl:key which can be used to group all matching elements together

    <xsl:key name="lines" match="reporting:line" use="
    concat(
     concat(
      concat(ancestor::reporting:folio[@reporting:level='1']/@reporting:name, ','),
      concat(ancestor::reporting:folio[@reporting:level='2']/@reporting:name, ',')
     ), 
     @reporting:name
    )" />
    

    Next, you need to select the first matching element in each group.

    <xsl:apply-templates select="//reporting:line
      [generate-id() = 
       generate-id(key('lines', ...concatenated key...  )[1])]" />
    

    Then, it is a “simple” case of summing up all elements that match the look-up key

    <xsl:value-of select="sum(key('lines', $keyName)/reporting:number)" />
    

    Putting this altogether gives

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:reporting="testing">
       <xsl:output method="text" indent="yes"/>
    
       <xsl:key name="lines" match="reporting:line" use="concat(concat(concat(ancestor::reporting:folio[@reporting:level='1']/@reporting:name, ','), concat(ancestor::reporting:folio[@reporting:level='2']/@reporting:name, ',')), @reporting:name)" />
    
       <xsl:template match="/">
          <xsl:apply-templates select="//reporting:line[generate-id() = generate-id(key('lines', concat(concat(concat(ancestor::reporting:folio[@reporting:level='1']/@reporting:name, ','), concat(ancestor::reporting:folio[@reporting:level='2']/@reporting:name, ',')), @reporting:name))[1])]" />
       </xsl:template>
    
       <xsl:template match="reporting:line">
          <xsl:variable name="keyName" select="concat(concat(concat(ancestor::reporting:folio[@reporting:level='1']/@reporting:name, ','), concat(ancestor::reporting:folio[@reporting:level='2']/@reporting:name, ',')), @reporting:name)" />
          <xsl:value-of select="$keyName" />
          <xsl:text>,</xsl:text>
          <xsl:value-of select="sum(key('lines', $keyName)/reporting:number)" />
          <xsl:text>&#13;</xsl:text>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the output is as follows:

    topfolder1,folder2,element1,625
    topfolder1,folder2,element2,665
    topfolder2,folder24,element3,65525
    topfolder2,folder24,element4,490
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table where i want to apply some aggregation functions having as
I want some slightly 'dynamic' XML source files. I want some of the element
I have some survey data that I want to describe by political party and
Just want some direction really. Want my search box to have text inside, but
I want some particular urls like springer.com to automatically convert to springer.com.proxy1.mycollege.edu. The second
I want some of the items to be bold depending on a property of
I want some examples of C preprocessor directives, such as: #define pi 3.14 #define
I want some code to be triggered every second. Usually, I'd create a Timer
I want some of the goodies in a ListView, like being able to use
I want some like this: How make it ideologically correct?

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.