RE: [xsl] Merge Similar data nodes using XSL

Subject: RE: [xsl] Merge Similar data nodes using XSL
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 16 Feb 2009 09:07:29 -0000
You need to look at "grouping". In XSLT 2.0 there is an xsl:for-each-group
construct which makes it quite easy; in XSLT 1.0 you need to learn about the
Muenchian grouping technique, which is quite tricky especially for newcomers
to the langauge. So first of all, find out whether you project has any
constraints that prevent you from using XSLT 2.0.

Using for-each-group, you could write something like this:

<xsl:for-each-group select="Contribution" group-by="AccountIDPub">
  <tr>
    <td><xsl:value-of select="AccountID"/></td> 
    <td><xsl:value-of select="AccountIDPub"/></td>
    etc (these select values from the first item in the group)

    To get the Comp Emp contribution:
    <xsl:value-of select="current-group()[ContributionType='Comp
Emp']/ContributionAmount"/>

    To get the total contribution:
    <xsl:value-of select="sum(current-group()/ContributionAmount)"/>

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Sunder Rajan [mailto:arun_9033@xxxxxxxxx] 
> Sent: 16 February 2009 07:03
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Merge Similar data nodes using XSL
> 
> Hi All,
> 
> I have posted this after a bit of research and being new to XSLT. 
> 
> There were similar clarifications in the past(FAQ) that 
> helped me to move  however I would appreciate if any one can 
> suggest me if I am heading in the right direction.
> 
> 1. Here is the XML sampple       
> <Contribution>
>          <AccountID>100</AccountID>
>          <AccountIDPub>173</AccountIDPub>
>          <EmployeeSurname>XXXX</EmployeeSurname>
>          <EmployeeFirstName>Sharon</EmployeeFirstName>
>          <EmployeeDOB>07/12/1955</EmployeeDOB>
>          <DateLastPaidTo>02/01/2009</DateLastPaidTo>
>          <ContributionType>Comp Emp</ContributionType>
>          <ContributionAmount>400.0</ContributionAmount>
>       </Contribution>       
> <Contribution>
>          <AccountID>100</AccountID>
>          <AccountIDPub>173</AccountIDPub>
>          <EmployeeSurname>XXXX</EmployeeSurname>
>          <EmployeeFirstName>Sharon</EmployeeFirstName>
>          <EmployeeDOB>07/12/1955</EmployeeDOB>
>          <DateLastPaidTo>02/01/2009</DateLastPaidTo>
>          <ContributionType>Personal</ContributionType>
>          <ContributionAmount>200.0</ContributionAmount>
>       </Contribution> 
> 
> 2. Key is <AccountIDPub> in the above nodes
> 
> 3. The <ContributionType> can have 5 variations value(1,2,3,4,5) 
>    The XML shows only 2 <ContributionType> it can have uptil 5.
>    The other I set them 0 for display (please see below)
> 
> The XSL I have written at the moment displays this as follows
> 
> AcNo. Name      DOB     Comp Emp   Volu   Salary  Personal 
> Spouse Total 
> --------------------------------------------------------------
> ----------
> 173   Sharon 07/12/1955 400.0      0        0        0      0 
>    400.0 
> 173   Sharon 07/12/1955   0        0       0       200.0    0 
>    200.0 
> 
> What would be the ideal way to get an output in a single row? 
> (Including adding the total)
> 
> AcNo. Name     DOB        Comp Emp  Volu  Salary Personal 
> Spouse  Total
> --------------------------------------------------------------
> ----------- 
> 173  Sharon 07/12/1962     400.0     0       0    200.0      
> 0    600.0 
> 
> 1. Should this be merged into a single XML first and then 
> displayed in a loop I guess I might need to do this in 2 passes...?
> 
> I understand that I can use a key (AcNo) and iterate, wasn't 
> sure if there were cleaner approach.
> 
> Any pointers appreciated....
> 
> Regards
> Sunder
> 
> 
>       Make Yahoo!7 your homepage and win a trip to the 
> Quiksilver Pro. Find out more

Current Thread