0

I am having a small problem parsing the tags in the nested namespace called "q0:", located below.

//Tag that I am trying to parse
  <q0:CustomerTransactionId>

I am able to get to all of the "v12:" namespaced tags but not the "q0:" ones for some reason.

Thanks!

Here is the XML

<?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <env:Header 
       xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<soapenv:Body>
<v12:ProcessShipmentReply 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:v12="http://fedex.com/ws/ship/v12">
    <v12:HighestSeverity>ERROR</v12:HighestSeverity>
    <v12:Notifications>
        <v12:Severity>ERROR</v12:Severity>
        <v12:Source>ship</v12:Source>
        <v12:Code>3058</v12:Code>
        <v12:Message>Recipient Postal code or routing code is required</v12:Message>
        <v12:LocalizedMessage>Recipient Postal code or routing code is required</v12:LocalizedMessage>
    </v12:Notifications>
    <q0:TransactionDetail xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q0="http://fedex.com/ws/ship/v12">
        <q0:CustomerTransactionId>21445634</q0:CustomerTransactionId>
    </q0:TransactionDetail><q0:Version xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q0="http://fedex.com/ws/ship/v12">
    <q0:ServiceId>ship</q0:ServiceId>
    <q0:Major>12</q0:Major>
    <q0:Intermediate>0</q0:Intermediate>
    <q0:Minor>0</q0:Minor>
  </q0:Version>
  </v12:ProcessShipmentReply>
  </soapenv:Body>
  </soapenv:Envelope>

And here is my parse

    $xml = simplexml_load_string($result, NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/');
    $xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xml->registerXPathNamespace('v12', 'http://fedex.com/ws/ship/v12');
    $xml->registerXPathNamespace('q0', 'http://fedex.com/ws/ship/v12');

    $bodies = $xml->xpath('env:Body');

    foreach($bodies as $body){

        $reply = $body->children('v12', TRUE)->ProcessShipmentReply;
        $reply2 = $body->children('q0', TRUE)->TransactionDetail;

        $custInfoArr['status'] = (string) $reply->HighestSeverity;

        if(strtolower($custInfoArr['status'])=="error"){


            $custInfoArr['invoiceNum'] = (string)$reply2->CustomerTransactionId;
            $custInfoArr['notificationSeverity']= (string) $reply->Notifications->Severity;
            $custInfoArr['notificationSource']= (string) $reply->Notifications->Source;
            $custInfoArr['notificationCode']= (string) $reply->Notifications->Code;
            $custInfoArr['notificationMessage']= (string) $reply->Notifications->Message;
            $custInfoArr['notificationLocalizedMessage']= (string) $reply->Notifications->LocalizedMessage;

        }

        $custInfoArr['trackingCode'] = (string) $reply->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;
        $custInfoArr['labelCode'] = (string) $reply->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image;
        $custInfoArr['labelCode'] = base64_decode($custInfoArr['labelCode']);

    }

1 Answer 1

1

<q0:TransactionDetail> is not a child of <env:Body>, it is a child of <v12:ProcessShipmentReply>, so you need to look for it inside $reply, not $body: $reply2 = $reply->children('q0', TRUE)->TransactionDetail;

The important thing to remember is that both the ->TagName operator and the ->children() method only look at immediate children of a particular node, they are not doing a "deep search" like XPath.

In fact, looking, both v12: and q0: are aliases for the same namespace ('http://fedex.com/ws/ship/v12'), so the next line can just be $reply2 = $reply->TransactionDetail. If you just say $reply = $body->children('http://fedex.com/ws/ship/v12')->ProcessShipmentReply rather than relying on the alias, this becomes clearer (and safer, since those aliases may change).

Incidentally, unless you're using it for something else, you can also get rid of all your XPath code (including all of the registerXPathNamespace calls) and just write $body = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body (I'm pretty sure SOAP only allows one Body per message).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I really appreciate your explanation

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.