Page 1 of 1

Sorting xml with simpleXML/xpath?

Posted: Tue Jul 20, 2010 12:38 pm
by RustyDoorknobs
Hey,

I have some xml, say:

Code: Select all

<Background>
 <Uses>14</Uses>
</Background>
<Background>
 <Uses>19</Uses>
</Background>
<Background>
 <Uses>3</Uses>
</Background>
How can I sort the xml from lowest Uses to highest?

Maybe an xpath expression?

Also, how could I just retrieve the bottom 2 Backgrounds, or the ones most recently added?

Thanks

Re: Sorting xml with simpleXML/xpath?

Posted: Tue Jul 20, 2010 4:17 pm
by Weirdan
RustyDoorknobs wrote: How can I sort the xml from lowest Uses to highest?
You can't sort using xpath alone - you can fetch your nodes into php arrays and sort those though.
Also, how could I just retrieve the bottom 2 Backgrounds,

Code: Select all

//Background[position()>last()-2]
or the ones most recently added?
There's no way to detect this given the xml you posted.

The bigger question, though, is why you're trying to use xml where database would be a natural choice?