Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Newbie: Sort Xml to Xml

From: "J. Larson" <xerious5@-------.--->
To: NULL
Date: 5/19/2009 1:30:00 AM
Here is a short xslt that will just sort by PileNo and copy the exact xml to 
the result tree.  The sort is done in the first match template with the copy 
occurring after the sort.  Also note that you will want to set the data-type 
on the sort or it will try to alpha sort instead.  You could also transform 
it into a different xml document after the sorting occurs.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="PileCoords">
		<xsl:copy>
			<xsl:apply-templates>
				<xsl:sort data-type="number" select="PileNo"/>
			</xsl:apply-templates>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="*">
		<xsl:copy>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

Hope this helps.
- Jeff

"Dave F." <df@b...> wrote in message 
news:u0d3Wup1JHA.1096@T......
> Hi
>
> I'm just starting out with XSL so please bear with me.
>
> I have an XML file in the following format:
>
> <PileCoords>
> <Pile>
> <PileNo>22</PileNo>
> <Eastings>57.7981568796959</Eastings>
> <Northings>166.059281023945</Northings>
> </Pile>
> <Pile>
> <PileNo>21</PileNo>
> <Eastings>57.7981568796959</Eastings>
> <Northings>161.461817940551</Northings>
> </Pile>
> <Pile>
> <PileNo>194</PileNo>
> <Eastings>68.6908869290537</Eastings>
> <Northings>167.952353915529</Northings>
> </Pile>
> <Pile>
> <PileNo>4</PileNo>
> <Eastings>83.9813040551062</Eastings>
> <Northings>167.952353915529</Northings>
> </Pile>
> </PileCoords>
>
> I would like to sort it by the PileNo element & keep it in XML format.
> I've seen many examples where it's transformed to another format but very 
> few to keep it as XML.
>
> In this example: 
> http://www.zvon.org/xxl/XSL-Ref/Tutorials/Sorting/so5.html
> The element is explicitly defined in the template. Is this essential?
> It would work in this case where I know all the elements names (eg PileNo, 
> Eastings Northings etc) but what if they'd varied in number & names?
>
> Is there a way to say 'copy all the elements under Pile regardless of what 
> they're called'?
>
> I hope I've used clear terminology.
>
> Any help to find a solution would be greatly appreciated.
>
> Thanks
> Dave F.
>
>
>
>
>
>
>
> 


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