可能重复:
在 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
,如上所述。