This seems like it would be a rather simple task.
XML given:
<items>
<item attr="1"/>
<item attr="2"/>
<item attr="3"/>
</items>
So typically:
$doc = new DOMdocument();
$doc->load($xml);
$xpath = new DOMXpath($doc);
$items = $xpath->query('/items/item');
foreach ($item as $item)
{
echo $item->getAttribute('attr');
}
So that would return: 123
So my question is, how would you query the document from the bottom such that results would be: 321
I did try using a for loop:
$numItems = $items->length;
for($i=$numItems;$i>0;$i--)
{
echo $items->item($i)->getAttribute('attr');
}
But I get an error on trying to access the attribute.
I'm sure there's a way to either:
1) modify the query to look from the bottom
2) use the for loop properly
3) last resort, actually reverse the nodeList but at the cost of further processing overheard
Any ideas would be greatly appreciated