OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Existing libraries/mechanisms for alphabetically sorting a

[ Lists Home | Date Index | Thread Index ]

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@CraneSoftwrights.com
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





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS