2012-03-12 58 views
0

這是我的SOAP XML文檔Body元素:嘗試查詢香皂:使用XPath

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。我如何調試這個混亂?

回答

0

好的,我發現我做錯了什麼。

由於標籤LoginResponse具有「xmlns」屬性,因此我需要在客戶端上使用前綴(我使用「x」)註冊屬性名稱空間值。這是像這樣做:

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

,其中 「HTTP://本地主機/ SPFService /」 是屬性 「的xmlns」 爲LoginResponse標記值。然後我可以使用xpathQuery來查找,即「/ soap:Envelope/soap:Body/x:LoginResponse」。請注意,我用「x:」前綴「LoginResponse」。