![]() |
sorting simplexml data by date
Need help with sorting simplexml data by date.
I'm pulling data from an xml feed with simplexml. But the problem is the output doesn't have any kind of ordering. I want to be able to sort them by date. (sort order being from now to future) How would I go about that? Date value is in this format: 2011-06-17 from $r25->start_date Here's how my code looks PHP Code:
|
There are lots of different ways to do this: here are three ideas.
|
Doing it with XSL would be great but it's not my xml feed.
I'll look into uasort (never used it before). Thanks in advance! |
It is not really important that it's not your XML feed, so long as PHP can find, read and load the XML then you can perform some XSL transformation on it (assuming your PHP has XSL enabled).
For example, the following loads the feed for the General forum here and sorts the items in the reverse order that they are presented in the feed. Then it just grabs the title and links. PHP Code:
Code:
<?xml version="1.0"?>Of course, precisely how the XML is loaded is up to you (e.g. using cURL to provide auth credentials. Also remember that once the new document is available as a DOMDocument (the return value from transformToDoc()) then you can switch over to using SimpleXML if that would make life easier (with the simplexml_import_dom() function) |
wow, i never knew XSLT can do this kind of stuff. thanks for showing me an example to learn from. i'll do some more reading on this.
Can you tell me how I would work with namespaces on xsl:for-each? say if my xml format was like so: <r25:events ...> <r25:event> bunch of stuff here </r25:event> </r25:events> Would it be like: <xsl:for-each select="/r25:events/r25:event"> ?? |
As with anything XML-ly, you'll need to make the stylesheet aware of the namespace which you can simply do with something like:
Code:
<xsl:stylesheet version="1.0" dc prefix with the http://purl.org/dc/elements/1.1/ namespace. Generally, it is a good idea to keep the namespace prefix the same as that used within the source XML just so you know what matches up with what; however it could equally be xmlns:foobar= even if the XML feed uses the dc prefix for that URI.Also, the last line ( exclude?) simple excludes the namespace from being part of the resulting output from the transformation: it's not required at all, experiment to see how the output is affected with and without it.As for how to refer to those namespaced elements, you've got the right idea. Use the prefix that you set up in the XSL file. P.S. Good luck with learning about this topic, it's a pretty huge one (and not one that I even pretend to know a lot about) so take it easy and read lots of material. |
Salathe, once again, thanks a million for introducing me to xslt. I played around with the code and got it working. I'm really starting to like XSL now.
If anyone wants to know how I got it to sort, here's what I did (hopefully it helps someone else): <xsl:sort select="r25:start_date" order="ascending" /> Also, I had to edit the $xml->load line to $xml->load('http://username:password@blah.com/blah.xml'); I tried to use $xml->load($feed); but it returned an error and I also tried $xml->loadxml($feed); but still got an error. I need to figure how I can check the url for errors prior to loading it. |
| All times are GMT. The time now is 01:17 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0