[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Existing libraries/mechanisms for alphabetically sorting a


xsl sort alphabetically
XSLT and XPath questions would be better posted to the following list:

   http://www.mulberrytech.com/xsl/xsl-list

There are a number of subscribers who would enthusiastically respond to 
such questions.

There is also an *excellent* FAQ at:

   http://www.dpawson.co.uk

At 2004-02-10 09:50 +0800, Peter Loh Yoon Chao wrote:
>I currently have XML files with a flat key-value structure and the elements
>are unsorted and without proper formatting, e.g.
>...
>Are there any existing libraries or mechanisms to do the following?
>- convert the file structure into a hierarchical tree structure

XSLT can be used for this.

>- alphabetically sorting the individual elements at each level

Your very specific requirements can be met with ease, but I'm concerned 
that you've not adequately described your true requirements ... I can only 
work from your example.

>- indent the individual nodes properly in the resulting XML file

Indentation in XML is cosmetic.  XSLT doesn't control indentation but 
implementations of XSLT can provide indentation when requested.  I ask 
myself why you might be interested in indentation ... how does this help 
your downstream processes?

>I would rather not reinvent the wheel if there are existing
>libraries/mechanisms to do this sort of thing.

An example is below that works with your data as supplied ... not sure if 
it will address your general case since that is not well described.  For 
example, I have not accommodated any namespaces you might have in your real 
data.

I hope this helps.

................................ Ken

p.s. I'm teaching two publicly-subscribed courses in Hong Kong in May if 
you are interested in going to the Island for some hands-on instruction.

t:\ftemp>type chao.xml
     <main>
<e.f.g>
   <value xml:lang="en">data 2</value>
         </e.f.g>
         <a.b.c>
<value xml:lang="en">data 1</value>
         </a.b.c>
         ...
      </main>

t:\ftemp>type chao.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/*">
   <xsl:copy>
     <xsl:copy-of select="@*"/>
     <!--assume all child names are three-components-->
     <xsl:for-each select="*">
       <xsl:sort select="substring-before(local-name(.),'.')"/>
       <xsl:sort select="substring-before(
                         substring-after(local-name(.),'.'),'.')"/>
       <xsl:sort select="substring-after(
                         substring-after(local-name(.),'.'),'.')"/>
       <xsl:element name="{substring-before(local-name(.),'.')}">
         <xsl:element name="{substring-before(
                             substring-after(local-name(.),'.'),'.')}">
           <xsl:element name="{substring-after(
                               substring-after(local-name(.),'.'),'.')}">
             <xsl:copy-of select="*"/>
           </xsl:element>
         </xsl:element>
       </xsl:element>
     </xsl:for-each>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>saxon chao.xml chao.xsl
<?xml version="1.0" encoding="utf-8"?>
<main>
    <a>
       <b>
          <c>
             <value xml:lang="en">data 1</value>
          </c>
       </b>
    </a>
    <e>
       <f>
          <g>
             <value xml:lang="en">data 2</value>
          </g>
       </f>
    </e>
</main>
t:\ftemp>


--
Public courses: upcoming world tour of hands-on XSL training events
Each week:    Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Washington, DC: 2004-03-15            San Francisco, CA: 2004-03-22
Hong Kong: 2004-05-17    Germany: 2004-05-24    England: 2004-06-07
World-wide on-site corporate, government & user group XML training!

G. Ken Holman                  mailto:gkholman@C...
Crane Softwrights Ltd.           http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0     +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness   http://www.CraneSoftwrights.com/x/bc


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.