RE: [xsl] group data in XSL

Subject: RE: [xsl] group data in XSL
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Wed, 18 Aug 2004 17:03:17 +0100
> Hi all,
>
> I have the following data and I would like to group
> them please help.
>
> <?xml version="1.0" standalone="yes"?>
> <dsWMSRpt>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Financial
> Div/Headquarters</ReportUnitTitle>
> 		<PositionNumber> 0934</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Part Div</ReportUnitTitle>
> 		<PositionNumber> 4563</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Financial Div</ReportUnitTitle>
> 		<PositionNumber> K948</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Human Resource
> Div</ReportUnitTitle>
> 		<PositionNumber> 6453</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>HumanResource Div</ReportUnitTitle>
> 		<PositionNumber> 9655</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Sale Div</ReportUnitTitle>
> 		<PositionNumber> SDWE</PositionNumber>
> 	</WMSReport>
> 	<WMSReport>
> 		<AdministrationCode>ABC</AdministrationCode>
> 		<ReportUnitTitle>Sale Div</ReportUnitTitle>
> 		<PositionNumber> 4577</PositionNumber>
> 	</WMSReport>
> </dsWMSRpt>
>
> Now I would like the output to group as following
>
> Administration Code: ABC
> Report Title: Financial Div
> Position Number: 0934
> Position Number: K948
>
> Administration Code: ABC
> Report Title: Part Div
> Position Number: 4563
>
> Administration Code: ABC
> Report Title: Sale Div
> Position Number: SDWE
> Position Number: 4577
>
> Administration Code: ABC
> Report Title: Human Resource Div
> Position Number: 6453
> Position Number: 9655

In XSLT 2.0 you can use:

    <xsl:for-each-group select="/dsWMSRpt/WMSReport"
group-by="ReportUnitTitle">
        Administration Code: <xsl:value-of select="AdministrationCode"/>
        Report Title:        <xsl:value-of select="ReportUnitTitle"/>
        <xsl:for-each select="current-group()/PositionNumber">
             Position Number:      <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:for-each-group>

You will have to sort out the whitespace issues, but you get the idea.
It's worth noting that your output doesn't match your input, because
"Financial Div/Headquarters" and "Financial Div" are different, as are
"Human Resource Div" and "HumanResource Div" when you've grouped using
them.

Cheers
andrew

Current Thread