Reordering Xml Child Nodes

J

javascript

I am wondering if it is possible to create a JavaScript function that
will re-order child nodes in an Xml Document. Currently I am binding
an Xml Data Island to an HTML table and implemented drag-and-drop
functionality.

Let's say I have the following:

Sequence 1
- 1
- 2
- 3
Sequence 2
- 4
- 5
- 6

The user re-orders so that:

Sequence 1
- 3
- 4
- 1

Sequence 2
- 5
- 6
- 2

Is it possible to sort each sequence's nodes from lowest to highest? I
have read about doing this with XSLT but figured I'd see if there was
an easier way.

Thanks in advance,

- will
 
M

Martin Honnen

I am wondering if it is possible to create a JavaScript function that
will re-order child nodes in an Xml Document. Currently I am binding
an Xml Data Island to an HTML table and implemented drag-and-drop
functionality.
Is it possible to sort each sequence's nodes from lowest to highest?

Sure you can do that, the DOM allows you to reorder nodes by simply
inserting them where you want them. To sort you can put the DOM element
nodes in a JavaScript array and use the sort method with an appropriate
function to compare the text content of an element as a number.
Here is a simple example doing that with items in an XML data island
<http://home.arcor.de/martin.honnen/javascript/2007/01/test2007012701.html>
Note that XML data islands are IE specific.
 
J

javascript

Thanks Martin. I am aware that this is IE Specific. The client I am
developing this for is a Microsoft shop so that means that all
computers will be running IE 5.x+
In my spare time I am working on a Mozilla-friendly version using
C# .NET's Xml & Repeater web controls.

Thanks again,

- will
 
J

javascript

What if I want to reorder Xml document using attributes? Each <item>
node will have multiple attributes. Let's say for sake of example I
want to reorder on itemId. How much more difficult would it be?

<root>
<product productId="1">
<item itemId="3" />
<item itemId="1" />
<item itemId="4" />
</product>
<product productId="2">
<item itemId="4" />
<item itemId="2" />
<item itemId="6" />
</product>
</root>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top