Re: [xsl] recursive sorting by element name

Subject: Re: [xsl] recursive sorting by element name
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 28 Nov 2007 17:46:15 -0500
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

Current Thread