By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Is it possible to sort a collection of XML childNodes?

P: n/a
Hi,

I am about to create a table, where the values are taken from an XML
file, where each column header you can click and it will sort the table
rows at the client side.

I have got to the following stage:
//first the xml file loading part
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML (xmlFile) {
xmldoc.load(xmlfile);
friends = xmldoc.documentElement;
}

loadXML ('friends.xml');
friends = friends.childNodes;
Below is a look at the XML file, friends.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<friends>
<person>
<firstname>Martin</firstname>
<surname>Bishop</surname>
<age>24</age>
</person>
<person>
<firstname>Robert</firstname>
<surname>Kennedy</surname>
<age>18</age>
</person>
<person>
<firstname>Nigel</firstname>
<surname>MacInnes</surname>
<age>23</age>
</person>
</friends>
Before I output the values of each node
(friends.childNodes[0].childNodes[0].text etc.), is it possible to
arrange / sort them by either firstname, surname or age?

Cheers

Burnsy

Aug 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
(continued)

When I say sort, I mean sort each <person> node by one of the child
nodes (firstname, surname or age).

For example, if I were to sort by age I would get the following order:

Robert
Nigel
Martin

Aug 17 '05 #2

P: n/a
bi******@yahoo.co.uk wrote:
Hi,

I am about to create a table, where the values are taken from an XML
file, where each column header you can click and it will sort the table
rows at the client side.


Typically records are sorted at the server when they are extracted from
the database so that they have some default (or maybe user defined)
order when first presented. Once you've created HTML from them, the
browser script does the sorting thereafter. You may have a cookie that
remembers the current sort column and uses it the next time the site is
visited.

There are a number of JavaScript-based table sort routines available,
some allow sorting of the table at the client when it is first loaded.

There are other techniques using XSL but last time I tried that (some
time ago) it was very browser-specific and slow. Things may have
improved but I think XSL/T has either moved to the server or been
largely ignored.
[...]
Reasonable table sort routine on Matt Kruze's site:

<URL:http://www.mattkruse.com/javascript/sorttable/>

Recent thread discussing table sort:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/tree/browse_frm/thread/d2e13db951e3db77/b3fdc2615655cf3d?rnum=1&hl=en&q=sort+table&_done=% 2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthre ad%2Fd2e13db951e3db77%2F757b0bb0092a4b7d%3Fq%3Dsor t+table%26rnum%3D2%26hl%3Den%26#doc_b3fdc2615655cf 3d>
--
Rob
Aug 17 '05 #3

P: n/a
RobG wrote:
Reasonable table sort routine on Matt Kruze's site:
<URL:http://www.mattkruse.com/javascript/sorttable/>


Although, I don't recommend my solution unless you're trying to do table
sorting and still support Netscape4. It was written way back in the day when
Netscape4 was a dominant browser, and client-side table sorting was a
difficult task. These days, if you don't need to support ancient browsers,
much nicer solutions exist.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 18 '05 #4

P: n/a
Matt Kruse wrote:
RobG wrote:
Reasonable table sort routine on Matt Kruze's site:
<URL:http://www.mattkruse.com/javascript/sorttable/>

Although, I don't recommend my solution unless you're trying to do table
sorting and still support Netscape4. It was written way back in the day when
Netscape4 was a dominant browser, and client-side table sorting was a
difficult task. These days, if you don't need to support ancient browsers,
much nicer solutions exist.


An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection, but it should be OK to replace that with row.childNodes
without any harm.

The 'ts_getInnerText' function uses innerText as on optimisation, it
could also test for and use textContent for browsers that support it.

<URL:http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent>
--
Rob
Aug 18 '05 #5

P: n/a
ASM
RobG wrote:

An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection,
No problemo, with Safari sorter links are disabled

but about Safari and/or row.cells collection
what is it ?

for me :
- the nth row collection from table 'tabl' could be something as :
row_n = document.getElementById('tabl').getElementsByTagNa me('TR')[n];
- and I don't know what is a row.cells collection
with Safari I can extract a TD from my 'row_n'
- how could be a nth col collection ?
could also test for and use textContent for browsers that support it.

<URL:http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent>


--
Stephane Moriaux et son [moins] vieux Mac
Aug 18 '05 #6

P: n/a
ASM wrote:
RobG wrote:

An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection,

No problemo, with Safari sorter links are disabled

but about Safari and/or row.cells collection
what is it ?


Try this test:

<table id="tableA">
<tr>
<td>Here is cell one</td>
<td>Here is cell two</td>
</tr>
</table>
<script type="text/javascript">
var t = document.getElementById('tableA');
alert(
"Rows collection supported? "
+ !!(t.rows)
+ "\nCells collection supported? "
+ !!(t.rows[0].cells)
+ "\nCells length available? "
+ !!(t.rows[0].cells.length)
+ "\nCan I access row 0 cell 0? "
+ !!(t.rows[0].cells[0])
+ "\nCan I access row 0 childNode 0? "
+ !!(t.rows[0].childNodes[0])
);
</script>

For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length

and

t.rows[0].cells[0].

But you can usually substitute the childNodes collection for the cells
collection quite happily without any side-effects as the only valid
children of a TR are TDs. The hard part is that Safari pretends to
support 'cells' but you can't access them.

for me :
- the nth row collection from table 'tabl' could be something as :
row_n = document.getElementById('tabl').getElementsByTagNa me('TR')[n];
- and I don't know what is a row.cells collection
with Safari I can extract a TD from my 'row_n'
- how could be a nth col collection ?


Play with table.rows and table.rows[n].cells (try in Firefox too, it
supports rows and cells collections).

[...]
--
Rob
Aug 18 '05 #7

P: n/a
ASM
RobG wrote:
ASM wrote:

but about Safari and/or row.cells collection
what is it ?

Try this test:


I did and did get : true on each line
For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length
and
t.rows[0].cells[0].
with my Safari 1.2.4 (v125.12) (Mac Os 10.3.7)

I get true for both
The hard part is that Safari pretends to
support 'cells' but you can't access them.
depends wich Safari ? (or Os X version ?)
I get "Here is cell one"
with :
<a href="javascript:alert(t.rows[0].cells[0].innerHTML)">test</a>
Play with table.rows and table.rows[n].cells (try in Firefox too, it
supports rows and cells collections).


I did it, and saw IE5.2 does too, all as iCab3
--
Stephane Moriaux et son [moins] vieux Mac
Aug 18 '05 #8

P: n/a
ASM wrote:
RobG wrote:
ASM wrote:

but about Safari and/or row.cells collection
what is it ?


Try this test:

I did and did get : true on each line
For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length
and
t.rows[0].cells[0].

with my Safari 1.2.4 (v125.12) (Mac Os 10.3.7)

I get true for both


Good, seems Apple have included support in 'Panther' and later. Pitty
they don't allow updating their browser independently of the OS (but
they aren't Robinson Cruso on that one...).

[...]

--
Rob
Aug 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.