473,804 Members | 4,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Xml only first level

5 New Member
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
7 2891
Dormilich
8,658 Recognized Expert Moderator Expert
what does the <xsl:sort> give you?
Oct 21 '09 #2
Amichai
5 New Member
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
Amichai
5 New Member
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 Recognized Expert Moderator Expert
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
Amichai
5 New Member
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 Recognized Expert Moderator Expert
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
Amichai
5 New Member
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
3632
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
4174
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) { return -1; } else
4
10040
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 have a .getxml method (unlike the dataset). any ideas? thanks.
2
1515
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 (i.e. Column1 DESC). Also I would need to be able to remove this from the last column that was sorted on so that it now says just Column2. <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import...
8
4151
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 repeater working, and paging/sorting works, but there is a small bug that I can't seem to figure out. If, for example, I display 5 records per page, the paging function executes fine, and only shows 5 records per page. This is no problem. When I...
4
3496
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 part) Within the unique Level1Id's, sort by Level2Id Within the unique level2Id's sort by name, then description. I looked at doing this with some foreach and list.FindAll() and copying the list into a new list, but this had some code smells....
4
1507
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 well aligned and close to its information on the level above. The only problem is that when i run the report it asks me to "ENTER PARAMETER VALUE" and shows the name of the new sorting level. Is there any way to eliminate this? is there any...
1
7193
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
3
7348
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
5
4961
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.