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 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
So, I have next situation: I have a page with form -> this form
I have three xml layout pages, 1.welcome screen (with next >> button) and clicking
I have a page that displays XML info in a table. The XML files
I have XML document that I parse with jDOM and then try to take
I have a JSON and XML based API that needs to be page cached.
I have next tables Order , Transaction , Payment . Class Order has some
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have next problem, when I'm trying to retrieve value from xforms:select elements I
I have next problem: i have a View: <UserControl x:Class=WpfApplication1.UserControl1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:d=http://schemas.microsoft.com/expression/blend/2008

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.