Jump to content

How Do I Filter Xml Elements By Date?


wattle40802

Recommended Posts

Hello, I am trying to write a programme to select data from my XML file by date. The example that I quote below is a database of services for our church which I would like to be presented on our web pages in such a way that only services newer than today's date or less far into the future than a date limit set by me (in this case 1 month) are visible to the viewer. Using the excellent tutorials and examples on www.w3schools.com I have been able to successfully make my XML data viewable on the web page, but not filtered by date. If anyone can tell me how to write my javascript to recognise the date attribute, I would be very grateful. Thanking you in advance. This is my XML file: <?xml version="1.0" encoding="ISO-8859-1" ?><servicesdiary> <service date="2011,10,27"> <rdate>27th November 2011</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>Advent</theme> </service> <service date="2011,10,27"> <rdate>27th November 2011</rdate> <time>6:30om</time> <type>Advent Evensong</type> <theme>Advent</theme> </service> <service date="2011,11,4"> <rdate>4th December 2011</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>Advent One</theme> </service> <service date="2011,11,11"> <rdate>11th December 2011</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>Advent Two</theme> </service> <service date="2011,11,18"> <rdate>18th December 2011</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>Advent Three</theme> </service> <service date="2011,11,25"> <rdate>25th December 2011</rdate> <time>09:30am</time> <type>Holy Communion</type> <theme>Christmas Day</theme> </service> <service date="2012,0,1"> <rdate>1st January 2012</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>Epiphany</theme> </service> <service date="2012,0,8"> <rdate>8th January 2012</rdate> <time>10:30am</time> <type>Holy Communion</type> <theme>The Baptism of Christ</theme> </service></servicesdiary> And this is my javascript script: xmlDoc=loadXMLDoc("ServicesDiary.xml"); var today=new Date();var ahead=new Date();ahead.setDate(ahead.getDate()+28); var x=xmlDoc.getElementsByTagName("service");document.write("<table border='2' width='700px' cellpadding='10'>");document.write("<tr><th>Date</th><th>Time</th><th>Type</th><th>Theme</th>");document.write("</tr>");for (i=0;i<x.length;i++){if ((x.item(i).attributes[0].value) < today) continue;if ((x.item(i).attributes[0].value) > ahead) continue; document.write("<tr><td>");document.write(x.getElementsByTagName("rdate")[0].childNodes[0].nodeValue);document.write("</td><td>"); document.write(x.getElementsByTagName("time")[0].childNodes[0].nodeValue);document.write("</td><td>"); document.write(x.getElementsByTagName("type")[0].childNodes[0].nodeValue);document.write("</td><td>"); document.write(x.getElementsByTagName("theme")[0].childNodes[0].nodeValue);document.write("</td></tr>"); }document.write("</table>");

Link to comment
Share on other sites

All attribute values from the XML are treated as strings, so there's no way for the comparison to be done on strings. You need to turn those strings into date objects.BTW, "attributes[0]" is a very bad way of accessing a particular attribute, because the order of attributes in that collection is unreliable. That is, even if you make sure your source has them as the first attribute, the browser may not follow the source order. Use getAttribute() instead.e.g.

var x=xmlDoc.getElementsByTagName("service");document.write("<table border='2' width='700px' cellpadding='10'>");document.write("<tr><th>Date</th><th>Time</th><th>Type</th><th>Theme</th>");document.write("</tr>");for (i=0;i<x.length;i++){var dateArray = x.item(i).getAttribute("date").split(",");var dateObject = new Date(dateArray[0], dateArray[1], dateArray[2]);if (dateObject  < today) continue;if (dateObject  > ahead) continue;

Link to comment
Share on other sites

Thank you, "boen_robot". I was beginning to reason that only strings were readable from XML documents so your suggestion of creating date objects makes sense. I will rewrite my script accordingly. Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...