Reputation: 357
I'm creating a simple web app to print some information contained on an XML test file using XPath. I show below this XML file (I've pointed some important values mentioned later):
This app is already capable to print some values, for example, if I want to print the green marked value on the screen capture I do this:
foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Emisor') as $emisor){
echo("Emisor = ".$emisor['nombre']);
}
Which prints "Emisor = PHARMA PLUS SA DE CV" that it's OK, after that, I want to print the red marked valued called "UUID" and I do this:
foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Complemento//tfd:TimbreFiscalDigital') as
$tfd){
echo("UUID = ".$tfd['UUID']);
}
Which gives me this message: SimpleXMLElement::xpath(): Undefined namespace prefix in /storage/ssd3...prueba4.php on line 46
I'm starting to use XML and php, would you help me to write the correct XPath please? Thanks in advance.
The XML file is: here
Upvotes: 0
Views: 196
Reputation: 57121
When using a namespace in XPath, you should register it with the (in SimpleXML's case) document using registerXPathNamespace()
.
So...
$xml->registerXPathNamespace("tfd", "http://www.sat.gob.mx/TimbreFiscalDigital");
foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Complemento//tfd:TimbreFiscalDigital') as
$tfd){
echo("UUID = ".$tfd['UUID']).PHP_EOL;
}
Gives...
UUID = ad662d33-6934-459c-a128-bdf0393e0f44
Upvotes: 2