473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting an XML document.

I have a slightly different sorting question than has been proposed before.
My docuement looks like:

<Records>
<Record>
<Sequence>5</Sequence>
<Name>E</Name>
</Record>
<Record>
<Sequence>1</Sequence>
<Name>A</Name>
</Record>
<Record>
<Sequence>4</Sequence>
<Name>D</Name>
</Record>
<Record>
<Sequence>2</Sequence>
<Name>B</Name>
</Record>
<Record>
<Sequence>3</Sequence>
<Name>C</Name>
</Record>
</Records>

Sorted it would look like:

<Records>
<Record>
<Sequence>1</Sequence>
<Name>A</Name>
</Record>
<Record>
<Sequence>2</Sequence>
<Name>B</Name>
</Record>
<Record>
<Sequence>3</Sequence>
<Name>C</Name>
</Record>
<Record>
<Sequence>4</Sequence>
<Name>D</Name>
</Record>
<Record>
<Sequence>5</Sequence>
<Name>E</Name>
</Record>
</Records>

The <Name> node is just some a sample in the real application the <Record>
has a lot of different data. I would like to sort the whole <Record> on
<Sequence>. So every thing that is in the <Record> would move based on the
value of <Sequence> so it is sorted. If I read this in a DOM and user XSLT
conceptually it is just moving nodes around but I end up with a kludgy
algorithm that removes nodes and inserts them and takes too much time. Any
ideas?

Thank you.

Kevin
Nov 12 '05 #1
1 2309
Kevin Burton wrote:
The <Name> node is just some a sample in the real application the <Record>
has a lot of different data. I would like to sort the whole <Record> on
<Sequence>. So every thing that is in the <Record> would move based on the
value of <Sequence> so it is sorted. If I read this in a DOM and user XSLT
conceptually it is just moving nodes around but I end up with a kludgy
algorithm that removes nodes and inserts them and takes too much time. Any
ideas?


Here is simple XSLT stylesheet that does the job:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="Records">
<Records>
<xsl:apply-templates select="Record">
<xsl:sort select="Sequence" data-type="number"
order="ascending"/>
</xsl:apply-templates>
</Records>
</xsl:template>
<xsl:template match="Record">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1145
by: reclusive monkey | last post by:
I copied the following example from MSDN, and it works nicely. However, I want to combine it with another technique I discovered on the IBM Developer site. However, the first method relies upon...
2
1875
by: Alan Searle | last post by:
I find that I can display structured data very nicely using XML with an XSL template. As an extra 'goodie', I would like to give users the ability to sort that data (for example with a button...
8
3487
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
2
6158
by: Alan Searle | last post by:
I would like to use an XSL/HTML template to sort XML data dynamically on the client side. As it is, I found a tutorial showing how to do this on the following site ... ...
7
4797
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
2
2478
by: felciano | last post by:
Hello -- I am trying to use XSL to process Amazon wishlist data to sort the results by type (Apparel, then Books, then DVDs, etc). Amazon's web services chunk up results in multiple pages of...
1
2982
by: Totti | last post by:
Hi everyone, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one textbox or datafile and store them in a 2 dimensional...
7
4184
by: Totti | last post by:
Hi all, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one tex area or data file and show the result in the 2nd text...
5
4902
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
7225
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
7326
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
7383
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...
1
7046
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
5627
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,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
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...

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.