2012-10-14 104 views
0

可能重複:
distinct in Xpath?XML複雜的分析

我想從使用PHP我的XML文件中提取數據。我想根據我的XML文件中的WebCategory得到唯一的ProductRange。但是下面編寫的PHP代碼會生成重複/重複的結果。我不知道我在哪裏犯錯!下面是代碼:

XML代碼:

<?xml version="1.0" standalone="yes"?> 
<Rows> 
<Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10001" Name="HTC Wildfire" ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10002" Name="Samsung Galaxy S3" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10003" Name="Samsung Galaxy S2" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10004" Name="Samsung Galaxy S1" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10005" Name="Samsung Galaxy Tabloid" ProductRange="Samsung" ProductSubRange="Galaxy Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10006" Name="Apple Ipad 3" ProductRange="Apple" ProductSubRange="Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10007" Name="Apple Iphone 4S" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10008" Name="Apple Iphone 3G" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10009" Name="Miscrosoft XBOX 360 Elite" ProductRange="Microsoft" ProductSubRange="XBOX" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10010" Name="Sony Playstation 4" ProductRange="Sony" ProductSubRange="Playstation" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10011" Name="Sony PSP Go" ProductRange="Microsoft" ProductSubRange="PSP" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10012" Name="Sony Erricsson Satio" ProductRange="Sony Ericsson" ProductSubRange="Satio Series" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
<Row Code="10013" Name="TomTom Go Live Gl2" ProductRange="TomTom" ProductSubRange="Go Live" WebCategory="Navigation" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12 Plt: 1152" /> 
</Rows> 

我想基於WebCategory從我的XML文件ProductRange獲得獨一無二的。但是下面編寫的PHP代碼會生成重複/重複的結果。我不知道我在哪裏犯錯!

PHP代碼:

<? 
$xml = simplexml_load_string(file_get_contents('XML/products.xml')); 
$prifix = '/categories/listings/' ; 
$cat=array(); 
foreach ($xml as $row) { 
$attr = $row->attributes(); 
if (!in_array((string)$attr->WebCategory, $cat)){ 
    printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->ProductRange)); 
    $cat[] = (string)$attr->WebCategory; 
} 
} 
?> 

請注意: 我想基於給定WebCategory例如,我想告訴根據自己Webcategory這樣選擇SQL查詢中的所有ProductRange提取ProductRange

 "select ProductRange from XML where WebCategory='Mobiles'" 

它可以給我不同的「ProductRange」(不重複的結果)基於這樣的XML:

HTC的
三星
iPhone

等等......我盡我所能,但未能產生使用上述編碼方法基於獨特的「ProductRange」。

請糾正我在哪裏我錯了,請引導我在哪裏我需要做出改變,以獲得獨特的ProductRange如上所述。

+1

怎麼樣XPath查詢? – JvdBerg

+2

該XML無效。你似乎錯過了'行'的開始標籤 – Quentin

+0

@Quentin對不起。我忘了在xml中添加,而write.i已經按照你所提到的那樣進行了編輯。有什麼辦法可以避免記錄中的重複? –

回答

1

您可以嘗試

$list = groupBy($xml, "WebCategory"); 
foreach ($list['Mobiles'] as $product) { 
    printf('<li>%s %s</li>', $product->Code, $product->Name); 
} 

輸出

  • 10000 HTC野火S-A510E
  • 10001 HTC野火
  • 10002三星Galaxy S3
  • 10003三星Galaxy S2
  • 10004三星Galaxy S1
  • 10007 Apple Iphone 4S
  • 10008 Apple Iphone 3G
  • 10012索尼Erricsson Satio的
  • 功能用於

    function groupBy($xml, $categoryName) { 
        $xml = new \SimpleXMLElement($xml); 
        $category = array(); 
        foreach ($xml as $row) { 
         $attr = $row->attributes(); 
    
         if (! isset($attr->$categoryName)) { 
          trigger_error("$categoryName does not exist in XML"); 
          break; 
         } 
    
         $category[(string) $attr->$categoryName][] = $attr; 
        } 
        return $category; 
    } 
    

    See live Demo

    +0

    輝煌&邏輯答案..謝謝你爸爸。我真的很感謝你help.bless –

    +0

    隨時歡迎您..是否有任何其他的事情,我可以幫助你@Bilal Khalid – Baba

    +0

    @ Baba.i已嘗試$ xml = simplexml_load_string(file_get_contents('XML/products.xml'));之前使用groupBy函數,因爲我嵌入外部XML文件,但它給了我錯誤。如果我使用查詢字符串的值,我將需要做什麼?我的意思是我使用codeigniter,並希望從我的uri segment.I取得不同的webcategories。感謝您的幫助.Bless –