Re: [xsl] Yet another grouping question

Subject: Re: [xsl] Yet another grouping question
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 08 Jan 2003 11:17:51 -0500
At 2003-01-08 08:17 -0700, Martinez, Brian wrote:
Now comes the grouping issue.  Cities served by multiple airports are
repeated in the table:

<tr>
  <td>Buenos Aires</td>
  <td>Jorge Newbery</td>
  <td>AEP</td>
</tr>
<tr>
  <td>Buenos Aires</td>
  <td>Ministro Pistarini</td>
  <td>EZE</td>
</tr>

Obviously I'd like both of these airports listed in one city node for Buenos
Aires, but I can't figure out how to modify my stylesheet to do this.  I
don't think I can use methods involving keys

Grouping isn't only accomplished through keys ... I often use variables and variables work across files.


I mocked up some data and the following seems to work fine in a small test.

I hope this helps.

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

T:\ftemp>type martinez1.xml
<?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="/">
  <html>
  <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td>
          <b class="title-destination">Algeria</b> <!-- country -->
          <table width="100%">
            <tr>
              <td>Algiers</td> <!-- city -->
              <td>Houari Boumedienne Arpt</td> <!-- airport name -->
              <td>ALG</td> <!-- airport code -->
            </tr>
            <tr>
              <td>Annaba</td>
              <td>Les Salines Arpt</td>
              <td>AAE</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>type martinez2.xml
<?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="/">
  <html>
  <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td>
          <b class="title-destination">Algeria</b> <!-- country -->
          <table width="100%">
            <tr>
              <td>Algiers</td> <!-- city -->
              <td>Houari Boumedienne Arpt</td> <!-- airport name -->
              <td>ALG-1</td> <!-- airport code -->
            </tr>
            <tr>
              <td>Annaba2</td>
              <td>Les Salines Arpt</td>
              <td>AAE</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>type martinez.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<file-names>
  <file>martinez1.xml</file>
  <file>martinez2.xml</file>
</file-names>

T:\ftemp>type martinez.xsl
<?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="td[1]"/>
        <xsl:if test="generate-id( . ) =
                      generate-id( $airport-nodes[ td[1]=current()/td[1] ] )">
          <country name="{../../b}">
            <city name="{td[1]}">
              <xsl:for-each select="$airport-nodes[ 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>
          </country>
        </xsl:if>
      </xsl:for-each>
    </airport-codes>
  </xsl:template>
</xsl:stylesheet>

T:\ftemp>saxon martinez.xml martinez.xsl
<?xml version="1.0" encoding="UTF-8"?>
<airport-codes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:noNamespaceSchemaLocation="airport_codes.xsd">
<country name="Algeria">
<city name="Algiers">
<airport code="ALG">Houari Boumedienne Arpt</airport>
<airport code="ALG-1">Houari Boumedienne Arpt</airport>
</city>
</country>
<country name="Algeria">
<city name="Annaba">
<airport code="AAE">Les Salines Arpt</airport>
</city>
</country>
<country name="Algeria">
<city name="Annaba2">
<airport code="AAE">Les Salines Arpt</airport>
</city>
</country>
</airport-codes>
T:\ftemp>rem Done!



-- 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