I have some problem when i try to read an xml from ro e-Factura sistem file with php, with this function: I have some problem when i try to read an xml from ro e-Factura sistem file with php, with this function:
function parseXMLFactura($path_xml) {
if (!file_exists($path_xml)) {
return [];
}
$xml = simplexml_load_file($path_xml, "SimpleXMLElement", LIBXML_NOCDATA);
$xml->registerXPathNamespace('cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
$xml->registerXPathNamespace('cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
$documentType = (string)$xml->getName();
$creditNoteTypeCode = (string)$xml->xpath('//cbc:CreditNoteTypeCode')[0];
$lineType = 'cac:InvoiceLine';
if ($documentType == 'CreditNote') {
$lineType = 'cac:CreditNoteLine';
}
$products = [];
foreach ($xml->xpath('//' . $lineType) as $line) {
$product = [];
$product['denumire'] = (string)$line->xpath('cac:Item/cbc:Name')[0];
$product['cod_produs'] = (string)$line->xpath('cac:Item/cbc:Description')[0];
if (empty($product['cod_produs'])) {
$product['cod_produs'] = $product['denumire'];
}
$product['cantitate'] = (string)$line->xpath('cbc:InvoicedQuantity')[0];
if (empty($product['cantitate'])) {
$product['cantitate'] = (string)$line->xpath('cbc:CreditedQuantity')[0];
}
$product['pret_buc'] = (string)$line->xpath('cac:Price/cbc:PriceAmount')[0];
$product['val_tva'] = (string)$line->xpath('cac:Item/cac:ClassifiedTaxCategory/cbc:Percent')[0];
$product['pret_raft'] = '0'; // Valoare implicită, actualizată ulterior dacă e cazul
$product['unitate_masura'] = (string)$line->xpath('cbc:InvoicedQuantity/@unitCode')[0];
if (empty($product['unitate_masura'])) {
$product['unitate_masura'] = (string)$line->xpath('cbc:CreditedQuantity/@unitCode')[0];
}
// Invert values for CreditNote with type code 381
if ($documentType == 'CreditNote' && $creditNoteTypeCode == '381') {
$product['cantitate'] = '-' . $product['cantitate'];
//$product['pret_buc'] = '-' . $product['pret_buc'];
}
$products[] = $product;
// Verificăm dacă există discounturi
foreach ($line->xpath('cac:AllowanceCharge') as $allowanceCharge) {
if ((string)$allowanceCharge->xpath('cbc:ChargeIndicator')[0] === 'false') {
$discount = [];
$discount['denumire'] = (string)$allowanceCharge->xpath('cbc:AllowanceChargeReason')[0];
$discount['cod_produs'] = $discount['denumire']; // Denumirea este folosită ca și cod produs pentru discount
$discount['cantitate'] = '-1';
$discount['pret_buc'] = (string)$allowanceCharge->xpath('cbc:Amount')[0];
$discount['val_tva'] = (string)$line->xpath('cac:Item/cac:ClassifiedTaxCategory/cbc:Percent')[0];
$discount['pret_raft'] = '0';
$discount['unitate_masura'] = '';
// Invert discount amount for CreditNote with type code 381
if ($documentType == 'CreditNote' && $creditNoteTypeCode == '381') {
$discount['pret_buc'] = '-' . $discount['pret_buc'];
}
$products[] = $discount;
}
}
}
return $products;
}
i receive: Warning: SimpleXMLElement::xpath(): Undefined namespace prefix
can someone help me to understand why ?
for some xml file work for another appeat error Warning: SimpleXMLElement::xpath(): Undefined namespace prefix