Re: [xsl] recursive sorting by element name

Subject: Re: [xsl] recursive sorting by element name
From: "Davis Ford" <davisford@xxxxxxxxx>
Date: Wed, 28 Nov 2007 17:54:25 -0500
Ken, I don't think you are missing anything.  That is spot-on.  Thanks
to everyone for the (quick) responses!

Regards,
Davis

On Nov 28, 2007 5:46 PM, G. Ken Holman <gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2007-11-28 17:04 -0500, Davis Ford wrote:
> >Hi, consider the following example:
> >...
> >I want to sort it as such
> >...
> >I am trying to come up with a generic XSLT solution that does not use
> >explicit element names, but only uses the sort select="name()", and it
> >should be recursive such that if say, element <A1> contained further
> >elements, then they would also be sorted by element name.
> >
> >Finally, if the element names are the same, then it should sort them
> >by an attribute called "typeName".
> >
> >Can anyone provide some help?  Thank you in advance!
>
> Am I missing something?  This is a variant of a one-template identity
> transform.
>
> I hope the example below helps.
>
> . . . . . . . . . . Ken
>
> t:\ftemp>type davis.xml
> <Collection>
>    <CollectionB>
>       <B2></B2>
>       <A2></A2>
>       <A1></A1>
>       <B1 typeName="2"></B1>
>       <B1 typeName="1"></B1>
>    </CollectionB>
>    <CollectionA>
>       <B2></B2>
>       <A2></A2>
>       <A1></A1>
>       <B1></B1>
>    </CollectionA>
> </Collection>
>
> t:\ftemp>type davis.xsl
> <?xml version="1.0" encoding="US-ASCII"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                  version="1.0">
>
> <xsl:output indent="yes"/>
> <xsl:strip-space elements="*"/>
>
> <xsl:template match="@*|node()">
>    <xsl:copy>
>      <xsl:apply-templates select="@*|node()">
>        <xsl:sort select="name(.)"/>
>        <xsl:sort select="@typeName"/>
>      </xsl:apply-templates>
>    </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>
> t:\ftemp>xslt davis.xml davis.xsl con
> <?xml version="1.0" encoding="utf-8"?>
> <Collection>
>     <CollectionA>
>        <A1/>
>        <A2/>
>        <B1/>
>        <B2/>
>     </CollectionA>
>     <CollectionB>
>        <A1/>
>        <A2/>
>        <B1 typeName="1"/>
>        <B1 typeName="2"/>
>        <B2/>
>     </CollectionB>
> </Collection>
> t:\ftemp>
>
>
>
>
> --
> Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
> World-wide corporate, govt. & user group XML, XSL and UBL training
> RSS feeds:     publicly-available developer resources and training
> G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
> Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
> Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
> Legal business disclaimers:  http://www.CraneSoftwrights.com/legal
>
>



-- 
Zeno Consulting, Inc.
http://www.zenoconsulting.biz
248.894.4922 phone
313.884.2977 fax

Current Thread