I need to sort the following XML (foreach ProgramList) based on the value of it's child MajorDescription
<ArrayOfProgramList xmlns="http://schemas.datacontract.org/2004/07/Taca.Resources" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ProgramList>
<AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
<a:AreaOfInterest>
<a:Interest>ABORIGINAL STUDIES</a:Interest>
</a:AreaOfInterest>
</AreaOfInterests>
<Coop>true</Coop>
<MajorDescription>ABORIGINAL COMMUNITY AND SOCIAL DEVELOPMENT</MajorDescription>
<Program>ACSD</Program>
<ProgramLocations>
<ProgramLocation>
<Campus>Barrie</Campus>
</ProgramLocation>
</ProgramLocations>
<Term>201210</Term>
</ProgramList>
<ProgramList>
<AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
<a:AreaOfInterest>
<a:Interest>GRADUATE CERTIFICATE STUDIES</a:Interest>
</a:AreaOfInterest>
<a:AreaOfInterest>
<a:Interest>HEALTH AND WELLNESS STUDIES</a:Interest>
</a:AreaOfInterest>
</AreaOfInterests>
<Coop>false</Coop>
<MajorDescription>ADVANCED CARE PARAMEDIC</MajorDescription>
<Program>PARM</Program>
<ProgramLocations>
<ProgramLocation>
<Campus>Barrie</Campus>
</ProgramLocation>
</ProgramLocations>
<Term>201210</Term>
</ProgramList>
</ArrayOfProgramList>
I'm trying to do it with SimpleDOM as I've read thats the easiest way to sort XML on other SO questions.
I've tried using:
foreach($listxml->sortedXPath('//ArrayOfProgramList/ProgramList','//ArrayOfProgramList/ProgramList/MajorDescription') as $program){ ... }
and various other similar 'sort' values such as '@MajorDescription', '/MajorDescription' and '.' as suggested here How does one use SimpleDOM sortedXPath to sort on node value? but everything returns an empty array when I check it with var_dump()
I think the problem is that I need to sort on the value of a child node - is this possible? The foreach needs to be on ProgramList as I need to output the values of all the child nodes within ProgramList on each iteration.
Any suggestions? I don't have to use SimpleDOM, I'm open to any method that works - currently I'm iterating through an array of A-Z, and for each letter, iterating the ProgramList, comparing the first letter of MajorDescription to the current letter and outputting if it matches - this is obviously not ideal and only sorts the first letter...