2

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;
mauzilla
  • 3,574
  • 10
  • 50
  • 86
  • For reference: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – FirstOne Jul 14 '16 at 17:38
  • If it's a duplicate I apologise, however, my query is related to retrieving a value from the domain namespace within the above document. The default namespace is epp so I am not sure how to "switch" namespaces (or if my logic even makes sense) :) – mauzilla Jul 14 '16 at 17:43
  • Don't worry, I didn't flag. It's good to leave references like these in case someone finds this question in the future looking for what's mentioned there. – FirstOne Jul 14 '16 at 17:45
  • FYI, you can't just type `epp:response` and get it working because namespace prefixes can change. What really identifies the tag is the namespace URI. Thus SimpleXML is not really as helpful as with regular XML. – Álvaro González Jul 15 '16 at 11:48
  • I honestly don't know how to best resolve this. It feels like quite a complex scenario for someone new to XML in general. I've tried to register both the namespaces with registerXpPathNamespace, have been able to access the epp namespace but no luck accessing the domain namespace – mauzilla Jul 15 '16 at 11:55

0 Answers0