Saturday, 27 December 2014

How to get a html table of an xml file in descending order based on a child node value



how could I read an xml file and print it as a html table in numerical order of a specific node? I have tried and tried many different approaches. xsl, xslt, jquery to sort it after its rendered and I am going nuts.


I need the people to be listed in the html table in order of highest points, currently it is the reverse of what i need.


my xml file is as follows



<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="reverse.xsl"?>
<POSTS>
<DICKHEAD>
<TITLE>#bono</TITLE>
<POINTS>1</POINTS>
</DICKHEAD>
<DICKHEAD>
<TITLE>#justinbieber</TITLE>
<POINTS>1</POINTS>
</DICKHEAD>
<DICKHEAD>
<TITLE>#katiehopkins</TITLE>
<POINTS>1</POINTS>
</DICKHEAD>
<DICKHEAD>
<TITLE>#georgebush</TITLE>
<POINTS>2</POINTS>
</DICKHEAD>
</POSTS>


and my html is...



<script>

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","https://dl.dropboxusercontent.com/u/434002/output.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;


document.write("<thead><table id='myTable' class='tablesorter'><tr><th>POINTS</th> <th>DICKHEAD</th></tr></thead><tbody>");
var x=xmlDoc.getElementsByTagName("DICKHEAD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td><h2>");

document.write(x[i].getElementsByTagName("POINTS")[0].childNodes[0].nodeValue);

document.write("</h2></td><td><h2>");

document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);

document.write("</h2></td></tr>");
}
document.write("</table>");
</script>


and here is my xsl file...



<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/POSTS/DICKHEAD">

<xsl:for-each select="descendant-or-self::*/POINTS">

<xsl:sort select="position()" data-type="number" order="descending"/>

<xsl:value-of select="@id"/>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>


Any help would be REALLY appreciated! Thanks


No comments:

Post a Comment