0

可能重复:
在 Xpath 中不同?

我正在尝试使用 PHP 从我的 XML 文件中提取数据。我想ProductRange根据WebCategory我的 XML 文件获得唯一性。但是下面编写的 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>

我想ProductRange根据WebCategory我的 XML 文件获得唯一性。但是下面编写的 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;
 }
 }
 ?>

请注意: 我想ProductRange根据给定的条件进行提取,WebCategory例如,我想根据他们的 Webcategory 显示所有 ProductRange,如下所示选择 SQL 查询:

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

它可以给我基于 XML 的不同的“ProductRange”(非重复结果),如下所示:

HTC
三星
iPhone

所以......我尽了最大努力,但未能使用上述编码方法生成独特的“ProductRange”。

请纠正我哪里错了,并请指导我需要在哪里进行更改以使其变得独一无二ProductRange,如上所述。

4

1 回答 1

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三星盖乐世S3
  • 10003三星盖乐世S2
  • 10004 三星盖乐世 S1
  • 10007 苹果 iphone 4S
  • 10008 苹果 iphone 3G
  • 10012 索尼爱立信 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;
    }
    

    观看现场演示

    于 2012-10-14T18:48:04.547 回答