Re: [xsl] Multiple groupings

Subject: Re: [xsl] Multiple groupings
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 27 Apr 2004 07:02:30 -0400
At 2004-04-27 00:13 -0400, Kenny Akridge wrote:
I basically have XML that has a PaymentType, City, Date and ID.  I
need to group all records by PaymentType then by City.

I find multi-level grouping is most easily solved using variable-based grouping.


I need to total by city and by Payment type.

I don't see any numbers to be totalled in your XML.


Any thoughts on the best way to tackle this?

Below is a working example with your data. Note how I use variables to capture the subset of the document in which I need to do grouping, so that I don't always have to deal with document-wide scope.


I hope this helps.

...................... Ken

t:\ftemp>type akridge.xml
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfAccountLineItems>
        <AccountLineItem>
                <ID>12993</ID>
                <PaymentType>Credit Card</PaymentType>
                <SettleDate>2004-04-14T22:57:46.6230000-04:00</SettleDate>
                <CityName>Las Vegas</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12992</ID>
                <PaymentType>Cash</PaymentType>
                <SettleDate>2004-04-14T22:57:46.6230000-04:00</SettleDate>
                <CityName>New York</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12963</ID>
                <PaymentType>Check</PaymentType>
                <SettleDate>2004-04-14T22:57:51.3100000-04:00</SettleDate>
                <CityName>Orlando</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12962</ID>
                <PaymentType>Check</PaymentType>
                <SettleDate>2004-04-14T22:57:51.3100000-04:00</SettleDate>
                <CityName>New York</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12969</ID>
                <PaymentType>Credit Card</PaymentType>
                <SettleDate>2004-04-14T22:57:51.4830000-04:00</SettleDate>
                <CityName>Las Vegas</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12968</ID>
                <PaymentType>Voucher</PaymentType>
                <SettleDate>2004-04-14T22:57:51.4830000-04:00</SettleDate>
                <CityName>Orlando</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12975</ID>
                <PaymentType>Check</PaymentType>
                <SettleDate>2004-04-14T22:57:51.6400000-04:00</SettleDate>
                <CityName>Las Vegas</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12974</ID>
                <PaymentType>Check</PaymentType>
                <SettleDate>2004-04-14T22:57:51.6400000-04:00</SettleDate>
                <CityName>Orlando</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12981</ID>
                <PaymentType>Voucher</PaymentType>
                <SettleDate>2004-04-14T22:57:51.8100000-04:00</SettleDate>
                <CityName>New York</CityName>
        </AccountLineItem>
        <AccountLineItem>
                <ID>12980</ID>
                <PaymentType>Cash</PaymentType>
                <SettleDate>2004-04-14T22:57:51.8100000-04:00</SettleDate>
                <CityName>Orlando</CityName>
        </AccountLineItem>
</ArrayOfAccountLineItems>


t:\ftemp>type akridge.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="items"
                select="/ArrayOfAccountLineItems/AccountLineItem"/>
  <xsl:for-each select="$items">
    <xsl:if test="generate-id(.)=
                  generate-id($items[PaymentType=current()/PaymentType])">
      <xsl:variable name="payments"
                    select="$items[PaymentType=current()/PaymentType]"/>
      <xsl:text/>Payments for '<xsl:value-of select="PaymentType"/>':
<xsl:text/>
      <xsl:for-each select="$payments">
        <xsl:if test="generate-id(.)=
                      generate-id($payments[CityName=current()/CityName])">
          <xsl:text/>  In city '<xsl:value-of select="CityName"/>:
<xsl:text/>
          <xsl:for-each select="$payments[CityName=current()/CityName]">
            <xsl:value-of select="concat('    ID:',ID,' Date:',SettleDate)"/>
            <xsl:text>
</xsl:text>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>saxon akridge.xml akridge.xsl
Payments for 'Credit Card':
  In city 'Las Vegas:
    ID:12993 Date:2004-04-14T22:57:46.6230000-04:00
    ID:12969 Date:2004-04-14T22:57:51.4830000-04:00
Payments for 'Cash':
  In city 'New York:
    ID:12992 Date:2004-04-14T22:57:46.6230000-04:00
  In city 'Orlando:
    ID:12980 Date:2004-04-14T22:57:51.8100000-04:00
Payments for 'Check':
  In city 'Orlando:
    ID:12963 Date:2004-04-14T22:57:51.3100000-04:00
    ID:12974 Date:2004-04-14T22:57:51.6400000-04:00
  In city 'New York:
    ID:12962 Date:2004-04-14T22:57:51.3100000-04:00
  In city 'Las Vegas:
    ID:12975 Date:2004-04-14T22:57:51.6400000-04:00
Payments for 'Voucher':
  In city 'Orlando:
    ID:12968 Date:2004-04-14T22:57:51.4830000-04:00
  In city 'New York:
    ID:12981 Date:2004-04-14T22:57:51.8100000-04:00

t:\ftemp>rem Done!



--
Public courses: Spring 2004 world tour of hands-on XSL instruction
Each week:   Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO

Hong Kong May 17-21; Bremen Germany May 24-28; Helsinki June 14-18

World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread