2011-08-21 27 views
0

我一直在試圖訪問某個XML,它看起來像這樣:無法讓使用XPath XML的選擇在PHP

<feed xmlns:s="http://syndication.nhschoices.nhs.uk/services" xmlns="http://www.w3.org/2005/Atom"> 
<title type="text">NHS Choices - GP Practices Near Postcode - W1T4LB - Within 5km</title> 
<entry> 
    <id>http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/27369</id> 
    <title type="text">Fitzrovia Medical Centre</title> 
    <updated>2011-08-20T22:47:39Z</updated> 
    <link rel="self" title="Fitzrovia Medical Centre" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/27369?apikey="/> 
    <link rel="alternate" title="Fitzrovia Medical Centre" href="http://www.nhs.uk/ServiceDirectories/Pages/GP.aspx?pid=303A92EF-EC8D-496B-B9CD-E6D836D13BA2"/> 
    <content type="application/xml"> 
    <s:organisationSummary> 
    <s:name>Fitzrovia Medical Centre</s:name> 
    <s:address> 
    <s:addressLine>31 Fitzroy Square</s:addressLine> 
    <s:addressLine>London</s:addressLine> 
    <s:postcode>W1T6EU</s:postcode> 
    </s:address> 
    <s:contact type="General"> 
    <s:telephone>020 7387 5798</s:telephone> 
    </s:contact> 
    <s:geographicCoordinates> 
    <s:northing>182000</s:northing> 
    <s:easting>529000</s:easting> 
    <s:longitude>-0.140267259415255</s:longitude> 
    <s:latitude>51.5224357586293</s:latitude> 
    </s:geographicCoordinates> 
    <s:Distance>0.360555127546399</s:Distance> 
    </s:organisationSummary> 
    </content> 
</entry> 
</feed> 

我一直在使用這個PHP代碼,試圖訪問數據:

<?php 

$feedURL = 'http://v1.syndication.nhschoices.nhs.uk/organisations/pharmacies/postcode/W1T4LB.xml?apikey=&range=5'; 
$xml = simplexml_load_file($feedURL); 
$xml->registerXPathNamespace('s', 'http://syndication.nhschoices.nhs.uk/services'); 

$pharm_entries = $xml->xpath('//entry'); 

var_dump($pharm_entries); 

foreach ($pharm_entries as $v) 
{ 
    print_r($v->nodeValue); 
} 
?> 

這裏$pharm_entries總是空的使用var_dump。出於某種原因,XPath沒有按原樣返回正確的SimpleXMLElement。我希望目前的//entry能夠找到<entry>節點的所有實例,但它不會......?

我怎樣才能使它工作?此外,獲取<s:address>標籤並打印出地址的每一行(仍使用XPath或其他方式)的最佳方法是什麼?

一如既往地欣賞幫助。

回答

2

嘗試過註冊了Atom命名空間:

$xml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom'); 

$pharm_entries = $xml->xpath('//a:entry'); 
+0

太好了,現在怎麼沒想到這一點:) ... – Alex