Re: [xsl] Customised sorting

Subject: Re: [xsl] Customised sorting
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 05 Apr 2001 09:51:55 +0100
At 01/04/05 14:30 +0200, Mille Eriksson wrote:
Now, my problem is very similar to this example but with the addition
that I need to sort the groups in a specific order. In the example this
would correspond to order the groups for example by the size of captial
of each country. This ordering information needs to bes supplied with
the style-sheet and not with the XML-document.

You don't pose very easy questions, do you? :{)}


Are there any ideas of how this can be accomplished?

The complete stylesheet below works successfully on your data. All I added was the population information and the single sort command.


Assuming the following population of capitals:

<pop:capitals>
 <city name="Paris"  pop="4000"/>
 <city name="Roma"   pop="3000"/>
 <city name="Madrid" pop="1000"/>
 <city name="Napoli" pop="2000"/>
</pop:capitals>

The result of executing the stylesheet is as follows:

T:\ftemp>saxon mille.xml mille.xsl
<?xml version="1.0" encoding="utf-8"?>
<countries>
   <country name="Espana">
      <city>Madrid</city>
      <city>Barcelona</city>
   </country>
   <country name="Italia">
      <city>Milano</city>
      <city>Firenze</city>
      <city>Napoli</city>
   </country>
   <country name="Italy">
      <city>Roma</city>
   </country>
   <country name="France">
      <city>Paris</city>
      <city>Nice</city>
      <city>Lyon</city>
   </country>
</countries>
T:\ftemp>

The sort value evaluation is as follows:

      <xsl:sort
         select="document('')/*/pop:capitals
                      /city[@name=current()/ancestor::cities/
                                    city[@country=current()]/@name]
                      /@pop"/>

... which in English reads:

"for each node being sorted, pick from the stylesheet capital nodes that city whose name attribute value is the same value as the name attribute of one of the cities in the source document of all those cities whose country attribute value is the same as the node being sorted, then base the sort on that stylesheet node's population attribute"

The finding of the capital of the country relies on the comparison of node sets.

I don't think there are any other ways to fulfill what you've asked for, but I would be pleased to see if there is a more succinct solution from another member of the mail list.

I hope you find this helpful.

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

<?xml version="1.0"?><!--mille.xsl-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:pop="population" exclude-result-prefixes="pop"
                version="1.0">

<pop:capitals>
 <city name="Paris"  pop="4000"/>
 <city name="Roma"   pop="3000"/>
 <city name="Madrid" pop="1000"/>
 <city name="Napoli" pop="2000"/>
</pop:capitals>

<xsl:output indent="yes"/>

<xsl:template match="/">
  <xsl:variable name="unique-countries"
    select="/cities
      /city[not(@country=preceding-sibling::city/@country)]
      /@country"
  />
  <countries>
    <xsl:for-each select="$unique-countries">
      <xsl:sort
         select="document('')/*/pop:capitals
                      /city[@name=current()/ancestor::cities/
                                    city[@country=current()]/@name]
                      /@pop"/>
      <country name="{.}">
        <xsl:for-each select="//city[@country=current()]">
          <city>
            <xsl:value-of select="@name"/>
          </city>
        </xsl:for-each>
      </country>
    </xsl:for-each>
  </countries>
</xsl:template>

</xsl:stylesheet>


p.s. please ignore my ignorance of European capital cities if I've guessed incorrectly



-- 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 (Fax:-0995) Web site: XSL/XML/DSSSL/SGML/OmniMark services, training, products. Book: Practical Transformation Using XSLT and XPath ISBN 1-894049-06-3 Article: What is XSLT? http://www.xml.com/pub/2000/08/holman/index.html Next public instructor-led training: 2001-04-06,05-01,05-14,05-15, - 05-16,05-17,05-21,05-22,06-18,06-21,07-20,07-21,09-19

!!Five-day XSLT/XPath/XSLFO Training Blitz June 18-22, 2001 in Ottawa!!


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



Current Thread