How can I sort XML nodes on multiple attributes using SimpleDom sortedxpath?
I'm using the SimpleDom library and need help with the sortedxpath function. Here's the XML
<cal>
<entry entry_id="1">
<entry_date year="1980" month="10" day="12" />
<entry_title>John Bday</entry_title>
</entry>
<entry entry_id="2">
<entry_date year="1980" month="10" day="10" />
<entry_title>Peter Bday</entry_title>
</entry>
<entry entry_id="3">
<entry_date year="1980" month="10" day="16" />
<entry_title>Allan Bday</entry_title>
</entry>
</cal>
I would like to sort all the 'entry' nodes according to the values of the 'year', 'month' and 'day' attributes of the 'entry_date' node, all that in reverse order (most recent on top). So in the example above, I'd like the final order to be:
entry #3
entry #1
entry #2
Here's the PHP I currently have which is not working (no output at all):
$xml = simpledom_load_file("data.xml");
foreach ($xml->sortedXPath('entry', 'entry_date_start[@year]', SORT_DESC) as $i => $item)
{
echo($item);
}
Any help开发者_运维百科 gladly appreciated. Thanks!
Finally found the correct syntax by trial and error:
foreach ($xml->sortedXPath('entry', 'entry_date/@year', SORT_DESC, 'entry_date/@month', SORT_DESC, 'entry_date/@day', SORT_DESC) as $i => $item)
{
// do stuff
}
I hope this can help others. Thanks for reading.
精彩评论