I fetch a lot of xml data and parse this via XPath. When writing the data to a file, I always get the enclosure tag in a wrong position additionally to the first enclosure.
Here is the code:
// define xpath expressions for the columns [] in v5.4+
$columns = array('Description' => 'normalize-space(cac:Item/cbc:Description)',
'SellersItemIdentification' => 'string(cac:Item/cac:SellersItemIdentification/cac:ID)',
'StandardItemIdentification' => 'string(cac:Item/cac:StandardItemIdentification/cac:ID)',
'ManufacturersItemIdentification' => 'normalize-space(cac:Item/cac:ManufacturersItemIdentification/cac:ID)',
'Producer' => 'normalize-space(cac:Item/cac:ManufacturersItemIdentification/cac:IssuerParty/cac:PartyName/cbc:Name)',
'PriceAmount' => 'string(cac:Item/cac:BasePrice/cbc:PriceAmount)',
'BaseQuantity' => 'string(cac:Item/cac:BasePrice/cbc:BaseQuantity)',
'Availability' => 'string(vco:Availability/vco:Code)');
// open a file stream for the standard output
if($startIndex == 0) {
//$csvStream = fopen('php://output', 'w');
$saveStream = fopen('import/'.$filename, 'w');
// write the columns names
fputcsv($saveStream , array_keys($columns), ';', '^');
} else {
//$csvStream = fopen('php://output', 'a');
$saveStream = fopen('import/'.$filename, 'a');
}
//iterate all items
foreach ($xpath->evaluate('//vco:ItemDetail') as $item) {
// a row for an item
$row = array();
foreach ($columns as $expression) {
// use the expression to fetch data for the columns and add it
$row[] = $xpath->evaluate($expression, $item);
}
// write to the output stream
fputcsv($saveStream, $row, ';', '^');
}
$startIndex = $startIndex + 500;
$i++;
The output is alway something like this:
^NAME^;39570934;0348039;....
So, the problem is, as you can see, the second mysterious enclosure which makes me a littlbe bit crazy.
Hope you can halp me