Page 1 of 1

PHP - Re-sorging XML doc with XSL

Posted: Tue Feb 05, 2008 11:45 am
by mcmcom
hi all,
im using xpath to return some search results as an XML DOM. Now i need to implement a sorting function which would take any number of parameters and sort the results based on them.
Essentially the parameters will be tags in the xml docs like "Name" and "category" for now.

So heres an example of an output from my search
<data>
<block>
<name>Test Name 2</name>
<category>Category 2</category>
</block>
<block>
<name>a Simple test</name>
<category>Category 1</category
</block>
</data>

So if that was returned we can see that block 2 with the name "a simple test" is in position 2. I want to apply a sort that will move that into position one and return a new xml doc. I read how to transform XML with XSL and understand how to replace nodes and values, but lost trying to understand how to apply any kind of sorting.

Any help would be appreciated.
TIA,
mcm

Re: PHP - Re-sorging XML doc with XSL

Posted: Tue Feb 05, 2008 12:46 pm
by Christopher
As I recall, there is not way to easily sort XML. I would convert the XML to an array, sort that, and then back to XML, then apply the XSL.

Re: PHP - Re-sorging XML doc with XSL

Posted: Tue Feb 05, 2008 2:35 pm
by mcmcom
thanks for the tip.
im a dot net guy usually. with dot net we have data sets and data tables which are quite easy to sort. Is there anything like that in php? maybe i can put the xml into a dataset first, apply sorts and filters and then spit it back out to xml?

thanks,
mcm

Re: PHP - Re-sorging XML doc with XSL

Posted: Tue Feb 05, 2008 2:38 pm
by Christopher
Take a look at the Simple XML library.

Re: PHP - Re-sorging XML doc with XSL

Posted: Tue Feb 05, 2008 2:48 pm
by mcmcom
using php 4 <ugh>

thanks
mcm