Connecting Tech Pros Worldwide Help | Site Map

Sorting Xml only first level

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: 4 Weeks Ago
Hi All.

I have an xml something like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.    <person>
  4.          <name>John</name>
  5.          <family name>Smith</family name>
  6.    </person>
  7.    <address>
  8.           <street>123</street>
  9.           <city>New York</city>
  10.    </address>
  11. </root>
I want to sort only the first level. i.e.
have result like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.    <address>
  4.           <street>123</street>
  5.           <city>New York</city>
  6.    </address>
  7.    <person>
  8.          <name>John</name>
  9.          <family name>Smith</family name>
  10.    </person>
  11. </root>
address now comes before person, but the internal nodes, keep their order.

I want to sort it alphabetically using xslt file.

Any idea?

I appreciate your help.


Amichai
best answer - posted by Amichai
Hi,

I discussed this issue with a friend of mine, and he suggested me not do this sorting with xslt, and do it using C# sorting function instead.

Dormilich, thanks for the help, and for the good willing.

Sorting first level using xslt is no more needed for my task.

Have you all nice week.

Amichai.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: 4 Weeks Ago

re: Sorting Xml only first level


what does the <xsl:sort> give you?
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: 4 Weeks Ago

re: Sorting Xml only first level


Hi Dormilich,

I'm pretty novice with xslt, and the examples i've seen over the web, shows how to sort the entire xml, but no one (i've find) show how to sort only first level.

The xslt file i'm using looks like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!-- ex: set sw=2 ts=2 expandtab: -->
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  4.  
  5.   <xsl:output method="xml" indent="yes" encoding="utf-8"/>
  6.  
  7.   <xsl:template match="/">
  8.     <xsl:apply-templates />
  9.   </xsl:template>
  10.   <xsl:template match="@*|node()">
  11.     <xsl:copy>
  12.       <xsl:copy-of select="@*"/>
  13.       <!-- copy all attributes before  applying templates to children only -->
  14.       <xsl:apply-templates >
  15.         <!-- First sort key: node name -->
  16.         <xsl:sort data-type="text" select="name()" order="ascending" case-order="upper-first" />
  17.         <!-- Second sort key: 'Name' property -->
  18.         <xsl:sort data-type="text" select="@Name" order="ascending" case-order="upper-first" />
  19.       </xsl:apply-templates>
  20.  
  21.     </xsl:copy>
  22.   </xsl:template>
  23. </xsl:stylesheet>
Newbie
 
Join Date: Oct 2009
Posts: 5
#4: 4 Weeks Ago

re: Sorting Xml only first level


actually, i'm trying to sort with node Name and 'Name' attribute, but for simplifying the question (as the sort according to one key or more is not relevant) i'm asking only about 'sort first level' issue.

Thanks for your help.

Amichai.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: 4 Weeks Ago

re: Sorting Xml only first level


looking at the provided xsl... why do you want to sort the XML? XML is used for data transfer, so a sort without intention behind does not make sense to me.
Newbie
 
Join Date: Oct 2009
Posts: 5
#6: 4 Weeks Ago

re: Sorting Xml only first level


I need to sort a very large xml file (sized 60MB).

Currently i'm sorting it using the xslt file above. however this consumes a lot of memory (~700MB), and i'm afraid that when i get larger xml files (let's say 200MB sized), the sorting will fail the application with out of memory exception.

So the solution i thought about was to sort only one level at time, and to travel over the xml and sort each time the node and the children without descended.

What i'm asking is: is there a simple way doing this with xslt?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#7: 4 Weeks Ago

re: Sorting Xml only first level


from what I know it is not specifically the sorting that requires that much memory, it is parsing and building the document tree itself that requires the memory. to work with very large XML files, you probably need a different parser model.
Newbie
 
Join Date: Oct 2009
Posts: 5
#8: 4 Weeks Ago

re: Sorting Xml only first level


Hi,

I discussed this issue with a friend of mine, and he suggested me not do this sorting with xslt, and do it using C# sorting function instead.

Dormilich, thanks for the help, and for the good willing.

Sorting first level using xslt is no more needed for my task.

Have you all nice week.

Amichai.
Reply

Tags
xml xslt sort one level