2011-04-02 67 views
1

我想在沒有遵循整個路徑的情況下找到xml響應中的一部分。現在我知道了XPath具有搜索能力,但不知何故,我不明白它... :(simplexml和使用相對路徑

我解析XML是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
     <wsaAction>http://www.rechtspraak.nl/namespaces/cir01/searchUndertakingResponse</wsaAction> 
     <wsaMessageID>urn:uuid:11f7d4cd-2280-4298-85eb-dadf5bd743f1</wsaMessageID> 
     <wsaRelatesTo>urn:uuid:59630fbd-b990-4020-9c1c-822c58186d96</wsaRelatesTo> 
     <wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo> 
     <wsseSecurity> 
      <wsuTimestamp wsu:Id="Timestamp-df25f141-fed2-47ed-967e-93cd04d1c8f2"> 
       <wsuCreated>2011-04-02T06:52:52Z</wsuCreated> 
       <wsuExpires>2011-04-02T06:57:52Z</wsuExpires> 
      </wsuTimestamp> 
     </wsseSecurity> 
    </envHeader> 
    <soapBody> 
     <searchUndertakingResponse xmlns="http://www.rechtspraak.nl/namespaces/cir01"><searchUndertakingResult> 
       <publicatieLijst xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" extractiedatum="2011-04-02T08:52:51" xmlns="http://www.rechtspraak.nl/namespaces/inspubber01"> 
        <publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk> 
        <publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk> 
       </publicatieLijst> 
      </searchUndertakingResult> 
     </searchUndertakingResponse> 
    </soapBody> 
</soapEnvelope> 

我期待這些值:<publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk> <publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>

現在這個工程:

$lijst = $results->soapBody->searchUndertakingResponse->searchUndertakingResult->publicatieLijst->publicatieKenmerk; 
    foreach ($lijst AS $kenmerk) { 
     echo $kenmerk."<BR>"; 
    } 

但是我不想用這個,因爲我需要靈活的其他結果,不能依靠 searchUn。 dertakingResponse-> searchUndertakingResult

,所以我希望使用XPath到那裏,但是這並不工作:

$lijst = $results->xpath('//publicatieKenmerk'); 
    foreach($lijst as $kenmerk) { 
     echo $kenmerk."<br />"; 
    } 

但我認爲相對會工作,以及...任何想法?

+0

我認爲這是對的preg_match和正則表達式或XML解析器的工作,但我不擅長這個。希望其他人會發布一段代碼。 – Wh1T3h4Ck5 2011-04-02 07:11:47

回答

2

你會發現,你的<publicatieLijst>節點有一個默認的命名空間集:xmlns="http://www.rechtspraak.nl/namespaces/inspubber01"這意味着<publicatieKenmerk>存在命名空間。這就是爲什麼//publicatieKenmerk找不到它,你必須在正確的命名空間中搜索。

對於這一點,你可以用你自己的前綴命名空間註冊和使用該前綴在下面的XPath查詢,例如:

$soapEnvelope = simplexml_load_string($xml); 

$soapEnvelope->registerXPathNamespace(
    'inspubber01', 
    'http://www.rechtspraak.nl/namespaces/inspubber01' 
); 

foreach ($soapEnvelope->xpath('//inspubber01:publicatieKenmerk') as $publicatieKenmerk) 
{ 
    echo $publicatieKenmerk, "\n"; 
} 
+0

這實際上甚至整潔的編碼...感謝那...代碼相應地調整.... – renevdkooi 2011-04-02 08:45:56

1

你可以嘗試改變他們的方式循環?

while(list(, $node) = each($lijst)) { 
    echo 'kenmerk: ',$node,"\n"; 
} 
+0

nope,仍然沒有... – renevdkooi 2011-04-02 07:35:48

+0

我認爲這是因爲不同的命名空間。嘗試用'http:// www.rechtspraak.nl/namespaces/inspubber01'命名空間 – ysrb 2011-04-02 07:43:16

+0

選擇節點我該怎麼做? – renevdkooi 2011-04-02 07:45:57