I need to access the value of an attribute located within a nested namespace in a XML document. Here is a sample of the XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0">
<epp:response>
<epp:result code="1000">
<epp:msg>Domain Check Command completed successfully</epp:msg>
</epp:result>
<epp:msgQ count="4" id="OTE-EPP-155DF5E824E-1BC86"/>
<epp:resData>
<domain:chkData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
<domain:cd>
<domain:name avail="1">example.com</domain:name>
</domain:cd>
</domain:chkData>
</epp:resData>
<epp:trID>
<epp:clTRID>123</epp:clTRID>
<epp:svTRID>456</epp:svTRID>
</epp:trID>
</epp:response>
</epp:epp>
In the above, I need to access the "avail" attribute which from what I can gather is located in a nested namespace domain within the epp namespace.
I am using simplexml_load_string($xml) to access the properties. I am able to access the epp namespace but my lack of XML knowledge is failing me on this one :)
Thank you in advance!
EDIT
As the question is marked as a duplicate I want to clear up that I am looking for a value of a property located within a nested namespace.
I have tried to access the property via xpath but have only received undefined namespace prefix errors:
echo $data->xpath('/domain:name')->attributes()->avail;
echo $data->xpath('/epp:response/epp:resData/domain:chkData/domain:cd/domain:name')->attributes()->avail;
I have also tried the above without the epp namespace but the same result. I know how to get the value from a single namespace but not from a nested namespace.
Another Edit:
I am still struggling. I really cannot get xpath to find the associated namespace element within the xml document. The below code is what I'm trying.
I have tried to also just do a vardump on $sxe->xpath('//d:name')) but get an empty array, indicating no values?
Please can someone help a brother out :)
$sxe = simplexml_load_string($result, "SimpleXMLElement", 0, "epp", true);
$code = $sxe->response->result->attributes()->code;
if ($code == 1000) {
$sxe->registerXPathNamespace('d','ietf:params:xml:ns:domain-1.0');
echo $sxe->xpath('//d:name')->attributes()->avail;
}
exit;
YET ANOTHER EDIT
Following another answer I changed my simplexml_load_string() function declaration and attempted to declare 2 xpath namespaces (feels like the closest I have gotten so far) - I am able to get the value in the epp namespace but still no luck with the domain namespace.
$sxe = simplexml_load_string($result,NULL,NULL,'urn:ietf:params:xml:ns:epp-1.0');
$sxe->registerXPathNamespace('epp', 'ietf:params:xml:ns:epp-1.0');
$sxe->registerXPathNamespace('domain', 'ietf:params:xml:ns:domain-1.0');
$code = $sxe->xpath('//epp:result')[0]->attributes()->code;
if ($code == 1000) {
print_r($sxe->xpath('domain:name'));
print_r($sxe->xpath('//domain:name'));
}
exit;