RE: [xsl] Sort an xml file by the using the ordering/sorting information from another xml file

Subject: RE: [xsl] Sort an xml file by the using the ordering/sorting information from another xml file
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 11 May 2006 09:45:25 +0100
Doing this is easy:

<xsl:sort
select="count(document('SortingOrder.xml')/*/MyElement[@key=current()/@key]/
preceding-sibling::*)" data-type="number"/>

Doing it efficiently may be a little more work: I would recommend creating a
copy of the SortingOrder file in which the elements are sequentially
numbered, and using a key to do the lookup.

Alternatively, if you know that all the sort keys appear in the SortingOrder
file, you could reverse the process: iterate over the SortingOrder file, and
for each element, find the elements having that key in the main file (by
means of a key lookup).

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: Jagsmiles [mailto:jagsmiles@xxxxxxxxx] 
> Sent: 11 May 2006 08:42
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Sort an xml file by the using the 
> ordering/sorting information from another xml file
> 
> I want to sort the elements of an xml file, The criteria of 
> ordering is actually specified in another xml file.
> 
> Can i use xsl to sort one xml file by the using the ordering 
> data from another xml file?:
> 
> I give a small e.g.
> 
> The Original xml file: main.xml
> 
> <myRoot>
>         <myElement key=3D"Tokyo" country=3D"JAP">I am 
> Tokyo</myElement>
>         <myElement key=3D"NewYork" country=3D"US">I am 
> NewYork</myElement>
>         <myElement key=3D"Sydney" country=3D"AUS">I am 
> Sydney</myElement>
>         <myElement key=3D"London" country=3D"UK">I am 
> London</myElement> </myRoot>
> 
> The Sorting Criteria xml file: SortingOrder.xml:
> <mySortingOrder>
>         <myElement key=3D"NewYork" />
>         <myElement key=3D"London" />
>         <myElement key=3D"Tokyo" />
>         <myElement key=3D"Sydney" />
> </mySortingOrder>
> 
> 
> After sorting the final xml data i should get is:
> <myRoot>
>         <myElement key=3D"NewYork" country=3D"US">I am 
> NewYork</myElement>
>         <myElement key=3D"London" country=3D"UK">I am 
> London</myElement>
>         <myElement key=3D"Tokyo" country=3D"JAP">I am 
> Tokyo</myElement>
>         <myElement key=3D"Sydney" country=3D"AUS">I am 
> Sydney</myElement> </myRoot>
> 
> in the final xml, the myElement elements are sorted according 
> to the sortin= g order specified in SortingOrder.xml.
> 
> Can i achieve this using xsl?
> Normally for the xsl:sort the sorting information should be 
> present in the element itself.
> 
> Thanks in advance.
> Jagdishwar.

Current Thread