【问题标题】:Trying to query soap:Body elements using xpath尝试使用 xpath 查询soap:Body 元素
【发布时间】:2012-03-12 16:08:54
【问题描述】:

这是我的 SOAP XML 文档:

HTTP/1.1 200 OK
Date: Mon, 12 Mar 2012 15:42:41 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 424

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <LoginResponse xmlns="http://localhost/SPFService/">
        <LoginResult>
          <Reply xmlns="">
            <SessionID>cdfbe607-59b1-4912-ae54-2190f1b7c7dd
            </SessionID>
          </Reply>
        </LoginResult>
      </LoginResponse>
    </soap:Body>
  </soap:Envelope>

现在我需要访问 SessionID 元素的 TextNode 值。我创建了一个 xpath 使用:

$this->dom->loadXML($response);
$this->xpath = new \DOMXPath($this->dom);

,其中 $response 是包含上述 xml 的字符串。

问题是我只能查询"/soap:Envelope""/soap:Envelope/soap:Body"。如果我尝试查询"/soap:Envelope/soap:Body/LoginResponse",我会得到一个空的DOMNodeList。我该如何调试这个烂摊子?

【问题讨论】:

    标签: php soap xpath domdocument xml-namespaces


    【解决方案1】:

    好的,我发现我做错了。

    由于标签 LoginResponse 有一个“xmlns”属性,我需要在客户端上注册带有前缀(我使用“x”)的属性名称空间值。这样做是这样的:

    $this->xpath = new \DOMXPath($this->dom);
    $this->xpath->registerNamespace('x', "http://localhost/SPFService/");
    

    ,其中“http://localhost/SPFService/”是 LoginResponse 标记的属性“xmlns”值。然后我可以使用 xpathQuery 来查找,即“/soap:Envelope/soap:Body/x:LoginResponse”。请注意,我在“LoginResponse”前面加上“x:”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-20
      • 2015-11-15
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多