我有一个类似于这样的 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<Root fileType="hierarchy" schema_version="1.1.1">
<group id="978407" subtype="Web Section">
<attribute definition_id="1294" is_name="true" type="text">
<data language="English" label="Web Section">3.6V Drill Drivers</data>
</attribute>
<group id="884622" subtype="Product">
<attribute definition_id="11" is_name="true" is_identifier="true" type="text">
<data language="English" label="Product Name">Bosch IXO IV Cordless Screwdriver 3.6V (1.5Ah)</data>
</attribute>
<attribute definition_id="15" type="text">
<data language="English" label="Brand">Bosch</data>
</attribute>
<attribute definition_id="18" type="text">
<data language="English" label="Model">IXO IV</data>
</attribute>
我想解析它并提取诸如“Model”值之类的内容(为了清楚起见,该值将是“IXO IV”或“id”属性的值(978407 或 884622)。
我希望使用 foreach 循环并将值添加到 csv 来完成此操作。
到目前为止我的 PHP 看起来像这样
<?php
// xml source
$filexml='3-6v-drill-drivers.xml';
if (file_exists($filexml))
{
// headers variable
$headers = array("sku", "attribute_set_code", "categories","name", "description","short_description","weight","speed","model","url_key","image","additional_images","accessories","alternatives");
//xml object var
$drillData = simplexml_load_file($filexml) or die("Me no understand");
$products = $drillData->xpath('/Root/group/group[@subtype="Product"]/attribute[@*]');
//create the file
$fh = fopen("testtttt.csv", "w");
//create headers
fputcsv($fh, $headers);
//iterate over object
foreach ($products as $product)
{
$put_arr = array($product->data['label="Model"'],$product->data['label="Brand"']);
fputcsv($fh, $put_arr);
}
fclose($fh);
}
?>
它不起作用...我也尝试在循环中使用 xpath 定义它($put_arr = array($product->xpath('/data/[@label="Brand"]')),也无济于事.
此 xpath 将在单个查询中获取请求的值
//group/@id | //group[@subtype="Product"]/attribute/data[@label="Model"]/text()'
id="978407"
ID=“884622”
IXO IV