0

There are a plenty answers to my question for example Parse XML namespaces with php SimpleXML But i cant understand how to adjust the code to read value in my case? i have a namespace m: inside a namespace soap: i need to parse this XML and only extract the value 888-0000019749

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
        <m:SaveDocumentsResponse xmlns:m="http://www.cargo3.ru">
            <m:return xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <m:Key>SaveDocuments</m:Key>
                <m:List>
                    <m:Key>Order</m:Key>
                    <m:Properties>
                        <m:Key>Number</m:Key>
                        <m:Value xsi:type="xs:string">888-0000019749</m:Value>
                        <m:ValueType>string</m:ValueType>
                    </m:Properties>
                    <m:Properties>
                        <m:Key>CreateDate</m:Key>
                        <m:Value xsi:type="xs:dateTime">2016-05-23T20:56:50</m:Value>
                        <m:ValueType>dateTime</m:ValueType>
                    </m:Properties>
                </m:List>
            </m:return>
        </m:SaveDocumentsResponse>
        </soap:Body>
</soap:Envelope>

Also can't manage to work this example parse an XML with SimpleXML which has multiple namespaces tried

$xml = simplexml_load_string($res, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('m', 'http://www.w3.org/2001/XMLSchema');

foreach($xml->xpath('//m:SaveDocumentsResponse') as $header)
{
    var_export($header->xpath('//m:return')); // Should output 'something'.

}

again I'm missing something...

and again

echo $list = (string)$xml->children('soap', true)->Body->children('m', true)->SaveDocumentsResponse->return->List;

i need the shortest and easiest way to extract value from

<m:Value xsi:type="xs:dateTime">2016-05-23T20:56:50</m:Value>
Community
  • 1
  • 1
vyazikov
  • 35
  • 1
  • 8

1 Answers1

0

You need to map a prefix to the appropriate namespace URI according to your XML data. So according to your XML, your prefix need to be mapped to URI 'http://www.cargo3.ru' :

$xml->registerXPathNamespace('m', 'http://www.cargo3.ru');

foreach($xml->xpath('//m:SaveDocumentsResponse') as $header)
{
    var_export($header->xpath('//m:return')); // Should output 'something'.
}

eval.in demo

output :

array (
  0 => 
  SimpleXMLElement::__set_state(array(
  )),
)
har07
  • 88,338
  • 12
  • 84
  • 137