Link to home
Start Free TrialLog in
Avatar of johnaryan
johnaryanFlag for Ireland

asked on

XSLT Grouping and Aggregation

This should be simple but I'm not very strong in XSLT.
I am trying to perform an XSLT on XML returned from a dataset that will give me the same
output as a "Group By" SQL statement combined with the "Sum" statement.

Using the Muenchian method i have been able to group by country but not Sum the Quantities

My XML Structure:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <ReportingDataId>2</ReportingDataId>
    <ReportingBusUnitDesc>United Kingdom</ReportingBusUnitDesc>
    <SystemQuantity>2</SystemQuantity>
    <TotalRevenue>10000</TotalRevenue>
  </Table>
  <Table>
      <ReportingDataId>2</ReportingDataId>
      <ReportingBusUnitDesc>United Kingdom</ReportingBusUnitDesc>
      <SystemQuantity>2</SystemQuantity>
      <TotalRevenue>10000</TotalRevenue>
  </Table>
  <Table>
      <ReportingDataId>2</ReportingDataId>
      <ReportingBusUnitDesc>Germany</ReportingBusUnitDesc>
      <SystemQuantity>4</SystemQuantity>
      <TotalRevenue>20000</TotalRevenue>
  </Table>
  <Table>
      <ReportingDataId>2</ReportingDataId>
      <ReportingBusUnitDesc>Germany</ReportingBusUnitDesc>
      <SystemQuantity>4</SystemQuantity>
      <TotalRevenue>20000</TotalRevenue>
  </Table>
</DataSet>

My XSL:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform ">

  <xsl:key name="countryKey" match="Table" use="ReportingBusUnitDesc"/>

  <xsl:template match="/">
    <table border="0" cellpadding="0" cellspacing="0" width="1000">
      <tr>
        <td class="header">
          Country
        </td>
        <td class="header">
          units
        </td>
        <td class="header">
          revenue
        </td>
      </tr>
     
      <xsl:call-template name="dataTableGroup" />
      <!-- Sum the Totals of quantities and revenue-->
      <xsl:call-template name="dataTotals" />
    </table>
  </xsl:template>

  <xsl:template name="dataTableGroup">
    <xsl:for-each select="NewDataSet/Table[generate-id()=generate-id(key('countryKey',ReportingBusUnitDesc))]">
      <xsl:sort select="ReportingBusUnitDesc"/>

      <tr>
        <td class="columnTextRight">
          <xsl:value-of select="ReportingBusUnitDesc"/>
        </td>
        <td class="columnData">
          <xsl:value-of select="sum(SystemQuantity[../ReportingBusUnitDesc=current()])"/>
        </td>
        <td class="columnData">
          $
        </td>
      </tr>

    </xsl:for-each>
  </xsl:template>
 
  <xsl:template name="dataTotals">
    <tr>
      <td class="footer">
        Total
      </td>
      <td class="footer" >
        <xsl:value-of select="sum(/NewDataSet/Table/SystemQuantity)" />
      </td>
      <td class="footer">
        $<xsl:value-of select="sum(/NewDataSet/Table/TotalRevenue)" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

My desired output is:

Country                   Quantity       Revenue
Germany                     8               40000
United Kingdom           4               20000
Total                           12              60000

If anyone could set me on the right path I would be very greatful.
Avatar of johnaryan
johnaryan
Flag of Ireland image

ASKER

I got this idea from another post on EE, it does the job....
I'm still looking the optimal performance out of this., so rather than give myself the points I'm leaving the post open.

My solution......

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="countryKey" match="Table" use="ReportingBusUnitDesc"/>
  <xsl:template match="/">
    <table border="0" cellpadding="0" cellspacing="0" width="600">
      <tr>
        <td class="header">
          Country
        </td>
        <td class="header">
          units
        </td>
        <td class="header">
          revenue
        </td>
      </tr>
      <xsl:call-template name="dataTableGroup" />
      <!-- Sum the Totals of quantities and revenue-->
      <xsl:call-template name="dataTotals" />
    </table>
  </xsl:template>

  <xsl:template name="dataTableGroup">
    <xsl:for-each select="NewDataSet/Table[generate-id()=generate-id(key('countryKey',ReportingBusUnitDesc))]">
      <xsl:sort select="ReportingBusUnitDesc"/>

      <tr>
        <td class="columnTextRight">
          <xsl:value-of select="ReportingBusUnitDesc"/>
        </td>
        <td class="columnData">
          <xsl:value-of select="sum(key('countryKey',ReportingBusUnitDesc)/SystemQuantity)"/>
        </td>
        <td class="columnData">
          $<xsl:value-of select="sum(key('countryKey',ReportingBusUnitDesc)/TotalRevenue)"/>
        </td>
      </tr>

    </xsl:for-each>
  </xsl:template>

  <xsl:template name="dataTotals">
    <tr>
      <td class="footer">
        Total
      </td>
      <td class="footer" >
        <xsl:value-of select="sum(/NewDataSet/Table/SystemQuantity)" />
      </td>
      <td class="footer">
        $<xsl:value-of select="sum(/NewDataSet/Table/TotalRevenue)" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Geert, I do appreciate the input.
I don't need too much optimizations as it seems the dataset will only be in the order of 10000 rows. And at the moment my sample set of 4000 performs adequately.