2012-10-14 138 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 –