开发者

Not able to make selection of XML using XPath in PHP

开发者 https://www.devze.com 2023-03-29 11:07 出处:网络
I\'ve been trying to access some XML, which looks like this: <feed xmlns:s=\"http://syndication.nhschoices.nhs.uk/services\" xmlns=\"http://www.w3.org/2005/Atom\">

I've been trying to access some XML, which looks like this:

<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>
   <开发者_Go百科;/s:organisationSummary>
  </content>
 </entry>
</feed>

I've been using this PHP code to try and access the data:

<?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);
}
?>

Here $pharm_entries is always empty using var_dump. For some reason XPath isn't returning a proper SimpleXMLElement with the query string as it is. I was hoping the current //entry would find all instances of <entry> nodes, but it doesn't...?

How can I get it to work? Also, what's the best way of going about getting the <s:address> tag, and printing out each line of the address (still using XPath or maybe another way)?

Appreciate the help as always.


Try registering the Atom namespace too:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消