2

I have this XML and I'm using SimpleXMLElment with xpath method to select first eSocial node:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header></soap:Header>
  <soap:Body>
    <eSocial xmlns="http://www.esocial.gov.br/schema/lote/eventos/envio/retornoEnvio/v2_2_02"> 
    ...
    </eSocial>  
  </soap:Body>
</soap:Envelope>

The search works when I use absolute xpath /soap:Envelope/soap:Body but when trying relative (//eSocial)[1] xpath I got no results. Code:

(new SimpleXMLElement($xml))->xpath('/soap:Envelope/soap:Body');
// SimpleXMLElement {#192}

(new SimpleXMLElement($xml))->xpath('(//eSocial)[1]');
// []

Which xpath should I use to get first occurrence of eSocial tag?

Alexandre Thebaldi
  • 4,546
  • 6
  • 41
  • 55
  • `eSocial` is in the `xmlns="http://www.esocial.gov.br/schema/lote/eventos/envio/retornoEnvio/v2_2_02"` namespace. Declare a prefix for it and then use it in your XPath. (See duplicate link for details.) Your XPath is otherwise fine wrt getting the first occurence. – kjhughes Jan 01 '18 at 15:58

0 Answers0