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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:51:52+00:00 2026-05-12T21:51:52+00:00

I have next xml: <page> <document> <id>1001</id> <cur>USD</cur> <date>01.01.2009</date> <amount>10</amount> </document> <document> <id>1001</id> <cur>USD</cur>

  • 0

I have next xml:

<page>
   <document>
      <id>1001</id>
      <cur>USD</cur>
      <date>01.01.2009</date>
      <amount>10</amount>
   </document>
   <document>
      <id>1001</id>
      <cur>USD</cur>
      <date>02.01.2009</date>
      <amount>15</amount>
   </document>
   <document>
      <id>1001</id>
      <cur>JPY</cur>
      <date>01.01.2009</date>
      <amount>5</amount>
   </document>
   <document>
      <id>1002</id>
      <cur>USD</cur>
      <date>01.01.2009</date>
      <amount>5</amount>
   </document>
   ...
</page>

And I need to transform it into html. Records should be grouped by id and cur. And after each group total amount should be shown. So we want something like this:

Bill: id=1001, cur=USD
      date=01.01.2009   amount=10
      date=02.01.2009   amount=15
      total amount=25
Bill: id=1001, cur=JPY
      date=01.01.2009   amount=5
      total amount=5
Bill: id=1002, cur=USD
      date=01.01.2009   amount=5
      total amount=5
...

How can I achieve this using XSL?

When I tried to find answer in google I found Muenchian method, but it’s too complicated when we want to group result by 2 fields. I’m beginner in xsl and it’s a bit difficult to me.
I also found xslt 2.0 operator for-each-group. Is it supported by major browsers? Is it normally to use it or we should only rely on xslt 1.0?

  • 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-12T21:51:52+00:00Added an answer on May 12, 2026 at 9:51 pm

    You can do this with XSLT 1.0

    The method i use here is to create a composite key with the two fields, id and cur. I later apply the templates to the first document in each group. Within the template i then loop through the individual documents and finally i summarize the documents amount field.

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:key name="idcur" match="document" use="concat(id,cur)"/>
    
    <xsl:template match="/page">
        <xsl:apply-templates select="document[generate-id() = generate-id(key('idcur',concat(id,cur))[1])]"/>
    </xsl:template>
    
    <xsl:template match="document">
    <xsl:variable name="document" select="key('idcur',concat(id,cur))"/>
    Bill: id=<xsl:value-of select="id"/>, cur=<xsl:value-of select="cur"/>
        <xsl:for-each select="$document">
          date=<xsl:value-of select="date"/>   amount=<xsl:value-of select="amount"/>
        </xsl:for-each>
          total amount=<xsl:value-of select="sum($document/amount)"/>
    </xsl:template>
    </xsl:stylesheet>
    

    Output:

    Bill: id=1001, cur=USD
          date=01.01.2009   amount=10
          date=02.01.2009   amount=15
          total amount=25
    Bill: id=1001, cur=JPY
          date=01.01.2009   amount=5
          total amount=5
    Bill: id=1002, cur=USD
          date=01.01.2009   amount=5
          total amount=5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document with companies listed in it. I want to create
I have a document with one CSS linked in. How can I replace the
I have two XML sources to retrieve data from. I want to use them
I have a Word .DOCument that's being generated by a (classic ASP) server. It's
I have encountered many half-solutions to the task of returning XML in ASP.NET. I
I have a custom feature which is an Edit Control Block (ECB) action in
I have a simple array with x number of items. I am displaying them
Can someone help me out on this. I have a problem with the return
First off, here is the situation. I'm using a guild hosting site that allows

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.