PHP - Re-sorging XML doc with XSL

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
mcmcom
Forum Newbie
Posts: 14
Joined: Thu Jan 31, 2008 7:23 am

PHP - Re-sorging XML doc with XSL

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP - Re-sorging XML doc with XSL

Post 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.
(#10850)
mcmcom
Forum Newbie
Posts: 14
Joined: Thu Jan 31, 2008 7:23 am

Re: PHP - Re-sorging XML doc with XSL

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP - Re-sorging XML doc with XSL

Post by Christopher »

Take a look at the Simple XML library.
(#10850)
mcmcom
Forum Newbie
Posts: 14
Joined: Thu Jan 31, 2008 7:23 am

Re: PHP - Re-sorging XML doc with XSL

Post by mcmcom »

using php 4 <ugh>

thanks
mcm
Post Reply