[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Yet another grouping question


Subject: Re: [xsl] Yet another grouping question
From: Niko Matsakis <niko@xxxxxxxxxxxxx>
Date: Wed, 08 Jan 2003 11:40:18 -0500

Another option besides the one I saw posted earlier is simply to run a second pass over your result document and use key.


Niko Matsakis DataPower Technology http://www.datapower.com

Martinez, Brian wrote:

I know grouping is discussed here ad nauseum, but I can't seem to apply what
I've read on the list and in the FAQ to my current problem.

I need to extract airport codes from 250+ stylesheets that were converted
from the original, static HTML pages.  I want to consolidate all of these
codes (some 3,500 in all) into an XML document so that I can use just *one*
stylesheet to display codes for a given country or U.S. state.

To automate the process, my source is just a list of the stylesheets
containing the airport code markup:

<file-names>
 <file>Albania.xsl</file>
 <file>Algeria.xsl</file>
 <file>Andorra.xsl</file>
 <file>Angola.xsl</file>
 etc.
</file-names>

I iterate through this list, loading each file via document(), then
transform its contents.  For foreign airports, the stylesheets all follow
the same table structure (some of the elements have been removed for
readability):

<xsl:template match="/">
 ...
 <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>
 ...
</xsl:template>

Transforming the above with my current stylesheet gives:

<country name="Algeria">
 <city name="Algiers">
   <airport code="ALG">Houari Boumedienne Arpt</airport>
 </city>
 <city name="Annaba">
   <airport code="AAE">Les Salines Arpt</airport>
 </city>
</country>

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, because they must match on a
known pattern, and my source is just the file list, not the individual
stylesheets.  My current stylesheet is pasted below; it's probably very
inefficient although performance isn't a priority (it only needs to run
once).  I realize this was very long but I appreciate any time you folks
spare on this.

thanks,
b.

| brian martinez                              brian.martinez@xxxxxxxx |
| senior gui programmer                                  303.708.7248 |
| trip network, inc.                                 fax 303.790.9350 |
| 6436 s. racine cir.                             englewood, co 80111 |
| http://www.cheaptickets.com/                   http://www.trip.com/ |

-- begin paste

<?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:for-each select="file-names/file">
<xsl:variable name="file-name" select="."/>
<xsl:variable name="airport-nodes"
select="document($file-name)/xsl:stylesheet/xsl:template[@match='/']/html/bo
dy"/>
<xsl:apply-templates select="$airport-nodes/table[1]"/>
</xsl:for-each>
</airport-codes>
</xsl:template>
<xsl:template match="table[1]/tr">
<country>
<xsl:attribute name="name"><xsl:value-of
select="td/b"/></xsl:attribute>
<xsl:apply-templates select="td/table[1]/tr/td[1]"/>
</country>
</xsl:template>
<xsl:template match="td[1]">
<city>
<xsl:attribute name="name"><xsl:value-of select="."/></xsl:attribute>
<xsl:apply-templates select="../td[3]"/>
</city>
</xsl:template>
<xsl:template match="td[3]">
<airport>
<xsl:attribute name="code"><xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="../td[2]"/>
</airport>
</xsl:template>


</xsl:stylesheet>

-- end paste

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






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




Current Thread
Keywords
xml