473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Sorting Xml only first level

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
Oct 21 '09 #1

✓ answered 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.

7 2876
Dormilich
8,658 Expert Mod 8TB
what does the <xsl:sort> give you?
Oct 21 '09 #2
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>
Oct 21 '09 #3
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.
Oct 21 '09 #4
Dormilich
8,658 Expert Mod 8TB
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.
Oct 21 '09 #5
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?
Oct 22 '09 #6
Dormilich
8,658 Expert Mod 8TB
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.
Oct 22 '09 #7
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.
Oct 25 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
4
by: suzy | last post by:
hello. how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't...
2
by: Mike P | last post by:
I have just written my first page with a datagrid that allows ASC and DESC sorting. But I want to know how I can get it so that the header shows the type of sorting that has just been done on it...
8
by: Matthew Curiale | last post by:
I am creating an app that lists clients of a company for management of different attributes for that company. The first page is a listing of the companies currently in the database. I have my...
4
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy...
4
by: Gilberto | last post by:
Hello, I have a report with some product information and i needed to create a NEW SORTING LEVEL with some TOTAL calculations which where located at the REPORT FOOTER, just so that they could be...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.