I am trying to read from an XML file which uses 3 namespaces and struggling to read values from it.
<?xml version ="1.0"?>
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
<cac:PartyName>
<cbc:Name>John Doe</cbc:Name>
</cac:PartyName>
My PHP so far after reading several examples, been years since ive written PHP so probably so easy for you guys.
<?php
$xml = simplexml_load_file('file.xml');
$xml->registerXPathNamespace('cbc',
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
$xml->registerXPathNamespace('cac',
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-
2');
foreach($xml->xpath('//cac:PartyName') as $PartyName)
{
print_r ($PartyName->xpath('//cbc:Name'));
}
?>
I register only two of the namespaces as they are the only ones in use in the XML file. (CBC and CAC). I get results from the entire array from this but i kinda want my output to be just "John Doe".