Is it possible to sort a collection of XML childNodes?

B

bissatch

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
 
B

bissatch

(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
 
R

RobG

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...sort+table&rnum=2&hl=en&#doc_b3fdc2615655cf3d>
 
M

Matt Kruse

RobG said:
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.
 
R

RobG

Matt said:
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>
 
A

ASM

RobG said:
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').getElementsByTagName('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 ?
 
R

RobG

ASM said:
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').getElementsByTagName('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).

[...]
 
A

ASM

RobG said:
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 :
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
 
R

RobG

ASM said:
RobG said:
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...).

[...]
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top