我想从使用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
如上所述。
怎么样XPath查询? – JvdBerg
该XML无效。你似乎错过了'行'的开始标签 – Quentin
@Quentin对不起。我忘了在xml中添加,而write.i已经按照你所提到的那样进行了编辑。有什么办法可以避免记录中的重复? –