Re: [xsl] recursive sorting by element name

Subject: Re: [xsl] recursive sorting by element name
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Nov 2007 17:45:02 -0500
Davis,

This problem is actually quite straightforward in XSLT, using an application of the "identity stylesheet" pattern, which works by traversing the document through the usual recursive descent and copying the input to the result as it goes.

Your only difference is that you will sort your elements as you descend.

So here's an identity template, one or another variant of which is common to this family of solutions:

<xsl:template match="*">
  <!-- matches any element -->
  <xsl:copy>
    <!-- copies the element -->
    <xsl:copy-of select="@*"/>
    <!-- copies its attributes, if any -->
    <xsl:apply-templates/>
    <!-- apply templates to the children -->
  </xsl:copy>
</xsl:template>

And here is a modification of it that does what you want:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates>
      <!-- apply templates to the children, writing results in
           order sorted by name and @typename of the input -->
      <xsl:sort select="name()"/>
      <xsl:sort select="@typename"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

Note: untested. But try that as the single template in your stylesheet and see what happens.

Cheers,
Wendell

At 05:04 PM 11/28/2007, you wrote:
Hi, consider the following example:

<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>

I want to sort it as such

<Collection>
  <CollectionA>
      <A1></A1>
      <A2></A2>
      <B1></B1>
      <B2></B2>
  </CollectionA>
  <CollectionB>
      <A1></A1>
      <A2></A2>
      <B1 typeName="1"></B1>
      <B1 typeName="2"></B1>
      <B2></B2>
  </CollectionB>
</Collection>

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!


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread