Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Simple Sort

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent