I am looking for a way to sort my xml data with javascript, and want to eventually filter out the data as well. I know all this is possible in the xsl file but i would like to do it client side.
I have searched multiple places for sorting with javascript but most of it was either too xml file specific or I couldn't figure out what was going on.
Would really appreciate any advice
I am looking for a way to sort my xml data with javascript, and want to eventually filter out the data as well. I know all this is possible in the xsl file but i would like to do it client side.
I have searched multiple places for sorting with javascript but most of it was either too xml file specific or I couldn't figure out what was going on.
Would really appreciate any advice
Share Improve this question asked May 11, 2009 at 20:27 fluffluf 1,2652 gold badges22 silver badges38 bronze badges 1- Sort your XML on what? What's your XML look like? – Nosredna Commented Jun 3, 2009 at 3:22
6 Answers
Reset to default 4The first part of this is performing the transformation in javascript:
function transformXML(_xml, _xsl) {
var
xml = typeof _xml == 'string'
? new DOMParser().parseFromString(_xml, 'text/xml')
: _xml // assume this is a node already
,xsl = typeof _xsl == 'string'
? new DOMParser().parseFromString(_xsl, 'text/xml')
: _xsl // assume this is a node already
,processor = new XSLTProcessor()
;
processor.importStylesheet(xsl);
return processor.transformToDocument(xml.firstChild);
}
This function accepts two params. The first is the xml that you want to transform. The second is the xslt that you want to use to transform the xml. Both params accept either strings that will be transformed to nodes or nodes themselves (such as XHR.responseXML).
The second part of the puzzle is sorting which you will use xsl's built-in xsl:sort
.
<xsl:sort
select="expression"
lang="language-code"
data-type="text|number|qname"
order="ascending|descending"
case-order="upper-first|lower-first"/>
All parameters are optional besides the select statement.
Sample sort usage:
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<xsl:value-of select="artist"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="title"/>
</xsl:for-each>
You can find more information about xsl:sort
at w3schools.
I wouldn't sort in the xsl sheet. I use the tablesorter plugin to jquery.
The Getting Started section is very straightforward(and is reproduced below).
To use the tablesorter plugin, include the jQuery library and the tablesorter plugin inside the tag of your HTML document:
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
tablesorter works on standard HTML tables. You must include THEAD and TBODY tags:
<table id="myTable">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Due</th>
<th>Web Site</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
<td>John</td>
<td>[email protected]</td>
<td>$50.00</td>
<td>http://www.jsmith.</td>
</tr>
<tr>
<td>Bach</td>
<td>Frank</td>
<td>[email protected]</td>
<td>$50.00</td>
<td>http://www.frank.</td>
</tr>
<tr>
<td>Doe</td>
<td>Jason</td>
<td>[email protected]</td>
<td>$100.00</td>
<td>http://www.jdoe.</td>
</tr>
<tr>
<td>Conway</td>
<td>Tim</td>
<td>[email protected]</td>
<td>$50.00</td>
<td>http://www.timconway.</td>
</tr>
</tbody>
</table>
Start by telling tablesorter to sort your table when the document is loaded:
$(document).ready(function()
{
$("#myTable").tablesorter();
}
);
Click on the headers and you'll see that your table is now sortable! You can also pass in configuration options when you initialize the table. This tells tablesorter to sort on the first and second column in ascending order.
$(document).ready(function()
{
$("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} );
}
);
A very good approuch is to have a XSL with parameer check inside of it for sorting and then apply a few js on it,a fully working example is available at:
http://www.google..br/search?hl=pt-BR&q=xml+javascript+data+sort&meta=
Why not just turn it into an array of objects and sort that, since you will probably need to convert it to display it anyway.
You may find some help here though if you must sort xml in javascript. http://www.velocityreviews./forums/t170211-clientside-filtering-and-sorting-xml-with-javascript-work-in-iebut-not-firefox.html
XSLT is supported on all major browser releases FYI ^_^ (IE 6+, FF 1+, SF 2+, CHROME, OPERA 9+)
not sure what your doing but XSLT can also be used in ajax
Well, why don't you use the prototype library? It has http://prototypejs/api/enumerable/sortBy It has http://prototypejs/api/enumerable/grep So why don't you just sort your elements and grep your elements? If you want that client-side in the javascript?