I'm parsing an XML file with LibXML and need to sort the entries by date. Each entry has two date fields, one for when the entry was published and one for when it was updated.
<?xml version="1.0" encoding="utf-8"?>
...
<entry>
<published>2009-04-10T18:51:04.696+02:00</published>
<updated>2009-05-30T14:48:27.853+03:00</updated>
<title>The title</title>
<content>The content goes here</content>
</entry>
...
The XML file is already ordered by date updated, with the most recent first. I can easily reverse that to put the older entries first:
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file);
my $xc = XML::LibXML::XPathContext->new($doc->documentElement());
foreach my $entry (reverse($xc->findnodes('//entry'))) {
...
}
However, I need to reverse sort the file by date published, not by date updated. How can I do that? The timestamp looks a little wonky too. Would I need to normalize that first?
Thanks!
Update:
After fiddling around with XPath namespaces and failing, I made a function that parsed the XML and stored the values I needed in a hash. I then used a bare sort
to sort the hash, which works just fine now.