[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Customised sorting
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
!!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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|