在SO和google上研究了几个小时之后......我希望在这里得到一些帮助: (我离运行正则表达式完全删除命名空间只有一步之遥)
首先是XML:
<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns="http://webservices.site.com/definitions">
<SessionId>0119A|1</SessionId>
</soap:Header>
<soap:Body>
<Security_AuthenticateReply xmlns="http://xml.site.com/QQ">
<processStatus>
<statusCode>P</statusCode>
</processStatus>
</Security_AuthenticateReply>
</soap:Body>
</soap:Envelope>
现在这就是我的PHP代码:
$response = simplexml_load_string( $str ,NULL,
false, "http://schemas.xmlsoap.org/soap/envelope/" );
// just making sure the name space is "registered"
// but I tested all examples also with this removed
$response->registerXPathNamespace("soap",
"http://schemas.xmlsoap.org/soap/envelope/");
$_res = $response->xpath('//soap:Header');
print_r($_res);
/*** result: simple query for the root "soap" namespace, this looks good! (so far..)
Array
(
[0] => SimpleXMLElement Object
(
[SessionId] => 0119A|1
)
)
***/
// now we query for the "SessionId" element in the XML
$_res = $response->xpath('//soap:Header/SessionId');
print_r($_res);
/*** result: this does not return anything!
Array
(
)
***/
// another approach
$_res = $response->xpath('//soap:Header/SessionId/text()');
print_r($_res);
/*** result: this does not return anything at all!
***/
// Finally, without using XPath this does work
$_res = $response->xpath('//soap:Header');
$_res = (string)$_res[0]->SessionId;
echo $_res;
/*** result: this worked
0119A|1
***/
如何使用XPATH ???
获取SOAP消息谢谢, 罗马
答案 0 :(得分:2)
多个名称空间正在弄乱它,为我添加以下作品
$response->registerXPathNamespace("site", "http://webservices.site.com/definitions");
$_res = $response->xpath('//site:SessionId');
另请参阅this之前的堆栈溢出问题
答案 1 :(得分:1)
您还需要注册<SessionId>
元素使用的默认命名空间。因为<SessionId>
在默认命名空间中,所以它没有任何前缀,但为了使XPath起作用,您还需要将此命名空间绑定到某个前缀,然后在XPath表达式中使用该前缀。
$response->registerXPathNamespace("ns",
"http://webservices.site.com/definitions");
$_res = $response->xpath('//soap:Header/ns:SessionId');
没有名称空间前缀的XPath(1.0)表达式始终只与非名称空间中的目标匹配。