Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Simple Sort >Thread Next - Re: Simple Sort Re: Simple SortTo: NULL Date: 5/1/2008 1:38:00 PM Techno_Dex wrote: > I have the following XML file which I want to sort by key then activedate > while maintaining the same order. I'm trying to generate this is a generic > format so that it can be used for slightly different cases in the future as > well. I have issues with the xsl:copy creating both a begin and end element > (i.e. <Record></Record>) as opposed to creating an empty element (i.e > <Record />) as well as getting all of the attributes copied without > explicitly identifying them. Whether an empty element is serialized as e.g. <foo/> or <foo></foo> or <foo /> is a serialization issue and not a question of how xsl:copy works. The XSLT instructions like xsl:copy work on an input tree and a result tree, not on markup. So if you have a 'Record' element marked up as <Record/> in your input document and then use xsl:copy to make a shallow copy to the result tree it does not ensure that the serialization of the result tree later looks the same as input markup. You will have to look into serialization options of your particular XSLT processor if you want to ensure a certain serialization of the result tree. > <RootElem> > <Record key="2" activedate="04/12/2008 02:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="1" activedate="04/13/2008 02:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="5" activedate="04/14/2008 12:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="3" activedate="04/14/2008 11:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="87" activedate="04/19/2008 02:30:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="64" activedate="04/13/2008 08:00:00" value="Test" attrib2= > "" attrib3="" /> > </RootElem> > > Output -> Sorted by key and activedate > ======= > <RootElem> > <Record key="1" activedate="04/13/2008 02:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="2" activedate="04/12/2008 02:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="3" activedate="04/14/2008 11:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="5" activedate="04/14/2008 12:00:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="87" activedate="04/19/2008 02:30:00" value="Test" attrib2= > "" attrib3="" /> > <Record key="64" activedate="04/13/2008 08:00:00" value="Test" attrib2= > "" attrib3="" /> > </RootElem> If you want to first sort on the key and then on the activedate attribute I wonder why key 87 comes before key 64. And the date format does not allow sorting unless you reorder it first, the following stylesheet sorts on yyyy-mm-ddThh:mm:ss <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="RootElem"> <xsl:copy> <xsl:apply-templates select="Record"> <xsl:sort select="@key" data-type="number"/> <xsl:sort select="concat(substring(@activedate, 7, 4), '-', substring(@activedate, 1, 2), '-', substring(@activedate, 4, 2), 'T', substring(@activedate, 12))" data-type="text"/> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
