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>
3
  • Hello, I don't know much about SimpleXml for that. I know how to use DomDocument Commented May 24, 2016 at 21:39
  • Hi! Any method will bring me some experience. Any working examples are welcome.. Commented May 24, 2016 at 22:09
  • Check examples bellow the documentation php.net/manual/en/domxpath.registernamespace.php Commented May 24, 2016 at 22:18

1 Answer 1

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(
  )),
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.