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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:24:18+00:00 2026-06-11T00:24:18+00:00

i have a XML datadump from a database that i want to present using

  • 0

i have a XML datadump from a database that i want to present using xslt.
The structure of the data is not ‘straightforward’ for the layout that i want (i also use the XML data for another report).

What i want is to do some calculations on data from Level-A where i need to group on Level-C children.

I know i can probably select the data again into an XML file where the structure is ‘easy’ for my report, but that is my last resort because i have the feeling that it can also be accomplished in XSLT itself. Most probbaly i need some ‘Muenchian’ trick to get it done, but since i am a ‘Muenchian Virgin’ i get stuck in every attempt (that i try to ‘steal’ and change …).

Does anybody know if Muenchian is the way to proceed and can somebody help me to get on the right track ?
I did some reading (including Jeni Tennison’s), but the stuff i have seen so far is not covering my problem as far as i know …

Below is a simplified XML structure that is (more or less) representative for my real problem.

Any ideas?

Kind regards, Henk

Simplyfied XML:

<data>
  <a>
    <a_id>A1</a_id>
    <a_desc>A one</a_desc>
    <a_val>1</a_val>
    <b>
      <c>
        <c_id>C2</c_id>
        <c_desc>C two</c_desc>
      </c>
    </b>
  </a>
  <a>
    <a_id>A2</a_id>
    <a_desc>A two</a_desc>
    <a_val>2</a_val>
    <b>
      <c>
        <c_id>C2</c_id>
        <c_desc>C two</c_desc>
      </c>
    </b>
  </a>
  <a>
    <a_id>A3</a_id>
    <a_desc>A three</a_desc>
    <a_val>3</a_val>
    <b>
      <c>
        <c_id>C1</c_id>
        <c_desc>C one</c_desc>
      </c>
    </b>
  </a>
  <a>
    <a_id>A4</a_id>
    <a_desc>A four</a_desc>
    <a_val>7</a_val>
    <b>
      <c>
        <c_id>C3</c_id>
        <c_desc>C three</c_desc>
      </c>
    </b>
  </a>
  <a>
    <a_id>A5</a_id>
    <a_desc>A five</a_desc>
    <a_val>11</a_val>
    <b>
      <c>
        <c_id>C1</c_id>
        <c_desc>C one</c_desc>
      </c>
    </b>
  </a>
</data>

Required output should be something like:

C_desc  Count() Sum(a_val)  Avg(a_val) 
------  ------- ----------  ----------
C one       3       15          5
C two       1       2           2
C three     1       7           7
  • 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-11T00:24:19+00:00Added an answer on June 11, 2026 at 12:24 am

    As you mention, Muenchian grouping is the way to go (in XSLT1.0). You say you want to group a elements, using values in a c element. Therefore you would define a key like so:

    <xsl:key name="a" match="a" use="b/c/c_desc" />
    

    Then, you need to get ‘distinct’ a elements, which is done by selecting a elements that happen to be the first element in the group for a given key. You do this with this fairly scary expression

    <xsl:apply-templates 
         select="//a[generate-id() = generate-id(key('a', b/c/c_desc)[1])]" />
    

    Here, key('a', b/c/c_desc)[1] will find the first element in the key’s group, and then you use generate-id to compare the elements.

    Then, you would have a template to match the distinct a elements, and within in this you can then do calculations on the group. For example, to get the sum:

    <xsl:value-of select="sum(key('a', b/c/c_desc)/a_val)" />
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="exsl">
       <xsl:output method="html" indent="yes"/>
       <xsl:key name="a" match="a" use="b/c/c_desc" />
       <xsl:template match="/">
          <table>
             <tr>
                <td>C_desc</td>
                <td>Count</td>
                <td>Sum</td>
                <td>Avg</td>
             </tr>
          <xsl:apply-templates select="//a[generate-id() = generate-id(key('a', b/c/c_desc)[1])]" />
          </table>
       </xsl:template>
    
       <xsl:template match="a">
          <xsl:variable name="c_desc" select="b/c/c_desc" />
          <tr>
             <td><xsl:value-of select="count(key('a', $c_desc))" /></td>
             <td><xsl:value-of select="sum(key('a', $c_desc)/a_val)" /></td>
             <td><xsl:value-of select="sum(key('a', $c_desc)/a_val) div count(key('a', $c_desc))" /></td>
          </tr>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <table>
       <tr>
          <td>C_desc</td>
          <td>Count</td>
          <td>Sum</td>
          <td>Avg</td>
       </tr>
       <tr>
          <td>3</td>
          <td>15</td>
          <td>5</td>
       </tr>
       <tr>
          <td>1</td>
          <td>2</td>
          <td>2</td>
       </tr>
       <tr>
          <td>1</td>
          <td>7</td>
          <td>7</td>
       </tr>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xml data that is retrieved from a server and I need to
I have xml http://weather.yahooapis.com/forecastrss?w=20070458&u=c and I want that when xml is updated my data
I have XML in the following form that I want to parse with PHP
I have XML data that looks like this <?xml version=1.0 encoding=utf-8?> <root> <account var1=
I have XML coming from a source that I can't change. They send it
I have xml files I want to read in and store in the database.
I have XML data which contains a long attribute value. The value does not
I have xml file with data that i have to display in table. Now
i have xml file which contains lots of data. now i want to pick
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...

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.