Using xpath on a PHP SimpleXML object, SOAP + namespaces (not working..)

后端 未结 2 940
我寻月下人不归
我寻月下人不归 2020-12-06 15:47

After researching this on SO and google for hours now... I hope to get some help here: (I am just one step away from running a regex to remove the namespaces completely)

相关标签:
2条回答
  • 2020-12-06 16:32

    You need to register the default namespace used by <SessionId> element as well. Because <SessionId> is in the default namespace it does not have any prefix but in order to your XPath to work, you need to bind also this namespace to some prefix and then use that prefix in your XPath expression.

    $response->registerXPathNamespace("ns",
        "http://webservices.site.com/definitions");
    $_res = $response->xpath('//soap:Header/ns:SessionId');
    

    XPath (1.0) expressions without a namespace prefix always match only to targets in no-namespace.

    0 讨论(0)
  • 2020-12-06 16:42

    The multiple namespaces are messing with it, adding the following works for me

    $response->registerXPathNamespace("site", "http://webservices.site.com/definitions");
    $_res = $response->xpath('//site:SessionId');
    

    also, see this previous stack overflow question

    0 讨论(0)
提交回复
热议问题