RE: [xsl] Yet another grouping question

Subject: RE: [xsl] Yet another grouping question
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 08 Jan 2003 12:29:34 -0500
At 2003-01-08 10:09 -0700, Martinez, Brian wrote:
returning a node-set from the tables in all files instead of
iterating through each file seems to make more sense.  However, that does
present a secondary grouping problem--your stylesheet outputs a separate
country node for each city.  I think combining your approach with Niko's
suggestion of doing a second pass on the output (and using a key) will do
the trick.

Not necessarily ... I think the modification below should work. You have all the information at hand in the source node tree. The code below groups by country name, and then within that subset groups by city name.


I hope this helps.

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

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <airport-codes>
      <xsl:attribute
name="xsi:noNamespaceSchemaLocation">airport_codes.xsd</xsl:attribute>

      <xsl:variable name="airport-nodes"
        select="document(file-names/file)/xsl:stylesheet/
                xsl:template[@match='/']/html/body/table[1]/tr/td/table/tr"/>
      <xsl:for-each select="$airport-nodes">
        <xsl:sort select="../../b"/>
        <xsl:if test="generate-id( . ) =
                      generate-id( $airport-nodes[ ../../b =
                                                  current()/../../b] )">
          <country name="{../../b}">
            <xsl:variable name="in-city" select="$airport-nodes[ ../../b =
                                                 current()/../../b]"/>
            <xsl:for-each select="$in-city">
              <xsl:if test="generate-id( . ) =
                            generate-id( $in-city[ td[1]=current()/td[1] ] )">
                <city name="{td[1]}">
                  <xsl:for-each select="$in-city[ td[1]=current()/td[1] ]">
                    <xsl:sort select="td[3]"/>
                    <airport code="{td[3]}">
                      <xsl:value-of select="td[2]"/>
                    </airport>
                  </xsl:for-each>
                </city>
              </xsl:if>
            </xsl:for-each>
          </country>
        </xsl:if>
      </xsl:for-each>
    </airport-codes>
  </xsl:template>
</xsl:stylesheet>


-- Upcoming hands-on in-depth North America: February 3- 7,2003 XSLT/XPath and XSL-FO Europe: February 17-21,2003

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)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1              Practical Formatting Using XSL-FO
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread