2013-01-10 29 views
0

我需要理清以下XML(的foreach ProgramList)的孩子MajorDescription如何使用PHP SimpleDOM對XML子節點值進行排序?基於它的價值(或任何其他方法)

<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> 

我試圖用SimpleDOM做到這一點,因爲我讀過的多數民衆贊成在其他SO問題上對XML進行排序的最簡單方法。

我已經嘗試使用:

foreach($listxml->sortedXPath('//ArrayOfProgramList/ProgramList','//ArrayOfProgramList/ProgramList/MajorDescription') as $program){ ... } 

和其他各種類似的 '排序' 值,如 '@MajorDescription', '/ MajorDescription' 和 ''在這裏建議How does one use SimpleDOM sortedXPath to sort on node value?但是當我用var_dump()檢查它時,所有的東西都會返回一個空的數組我認爲問題是我需要對子節點的值進行排序 - 這可能嗎? foreach需要在ProgramList上,因爲我需要在每次迭代時輸出ProgramList中所有子節點的值。

有什麼建議嗎?我不必使用SimpleDOM,我可以使用任何可用的方法 - 目前我正在迭代AZ數組,迭代ProgramList,將MajorDescription的第一個字母與當前字母進行比較,然後對每個字母進行迭代輸出,如果它匹配 - 這顯然是不是的理想,只有第一個字母排序...

回答

1

您可以嘗試將所有的ProgramList元素放入一個數組,並根據自定義函數進行排序。該代碼應該是這樣的:

function cmp($a, $b) 
{ 
    return strcmp($a->MajorDescription[0],$b->MajorDescription[0]) 
} 

$arr = $listxml->xpath("//ArrayOfProgramList/ProgramList"); 
usort($arr,"cmp"); 
+0

謝謝 - 你的答案激勵我回頭看看usort - 我無法完全用你的代碼工作,但它通過使用這個問題的答案使用usort工作http://stackoverflow.com/questions/8274193/排序XML-DIV逐子節點-PHP-的SimpleXML – tsdexter

0

有兩個問題與你的原代碼。首先,您的XML使用默認命名空間,並且按照設計,XPath不支持默認命名空間,因此您必須查找名稱空間節點(例如//foo:bar,而不是//bar)以查找它們。如果您無法爲此名稱空間註冊前綴(例如,如果無法修改源XML),則可以使用通配符//*與名稱空間節點匹配,該名稱空間節點與匹配節點名稱空間和/或本地名稱的謂詞相結合。

$nsPredicate = '[namespace-uri() = "http://schemas.datacontract.org/2004/07/Taca.Resources"]'; 

$query = '//*[local-name() = "ArrayOfProgramList"]' . $nsPredicate 
     . '/*[local-name() = "ProgramList"]' . $nsPredicate; 

$orderBy = '*[local-name() = "MajorDescription"]' . $nsPredicate; 

foreach ($listxml->sortedXPath($query, $orderBy) as $program) 
{ 
    echo $program->asXML(),"\n"; 
} 

另一個問題是您的排序標準。它應該從目標節點的上下文寫入。

相關問題