Использование XPath для выбора пространств имен

Я пытался использовать XPath в PHP для доступа к Atom. канал из API Национальной службы здравоохранения.

Данные выглядят следующим образом:

<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   

$feedURL = 'http://v1.syndication.nhschoices.nhs.uk/organisations/pharmacies/postcode/W1T4LB.xml?apikey=&range=5';
$xml = file_get_contents($feedURL);
$sxml = new SimpleXMLElement($xml);
$sxml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$sxml->registerXPathNamespace('s', 'http://syndication.nhschoices.nhs.uk/services');

$addr = $sxml->xpath('//s:address');

var_dump($addr[0]);

?>

Здесь $ addr пуст, но должен иметь десять записей.

Кто-нибудь может объяснить, как правильно распечатать содержимое каждого узла и поместить почтовый индекс в переменную.

Я работаю с принципом пространства имен на практике, хотя это для меня довольно ново. Цените любую информацию, которую вы могли бы сообщить об изучении XPath и модели PHP SimpleXML.

Цените помощь.

РЕДАКТИРОВАТЬ -

Увидев приведенное ниже обновление, я решил чтобы поместить мой окончательный выходной код в это:

function doPharmacy($postcode, $request, $prev, $next)
{


    $feedURL = 'http://v1.syndication.nhschoices.nhs.uk/organisations/pharmacies/postcode/' . $postcode . '.xml?apikey=&range=5';
    $xml = file_get_contents($feedURL);
    $sxml = new SimpleXMLElement($xml);
    $sxml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
    $sxml->registerXPathNamespace('s', 'http://syndication.nhschoices.nhs.uk/services');

    ////////////// XPATH \\\\\\\\\\\\\\

    $addrLines = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:content/s:organisationSummary/s:address/s:addressLine');
    $nhsPostcode = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:content/s:organisationSummary/s:address/s:postcode');
    $tel = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:content/s:organisationSummary/s:contact/s:telephone');
    $distance = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:content/s:organisationSummary/s:Distance');
    $title = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:title');
    $link = $sxml->xpath('/a:feed/a:entry[' . $request . ']/a:link[@rel="alternate"]/@href');

    $nhsPostcode = array_pop($nhsPostcode); // always taking first node from set
    $tel = array_pop($tel);
    $distance = (double)array_pop($distance);
    $title = array_pop($title);
    $link = array_pop($link);


    ////////////// OUTPUT: JSON \\\\\\\\\\\\\\

    print '{"addr": "';

    foreach ($addrLines as $addr)
    {
        print $addr . '<br />'; // ok to use HTML tags in JSON
    }

    print '",';

    print '"postcode": "' . $nhsPostcode . '",';

    print '"tel": "' . $tel . '",';

    $num = number_format($distance, 2);

    print '"distance": "' . $num . '",';

    print '"title": "' . $title . '",';

    print '"link": "' . $link . '",';

    $nhsPostcode = urlencode($nhsPostcode);

    print'"mapsURL": "http://maps.googleapis.com/maps/api/staticmap?center=' . $nhsPostcode . '&zoom=15&size=155x137&sensor=false&scale=2",';

    print '"prev": "' . $prev . '",';

    print '"next": "' . $next . '"';

    print '}';
}

?>
6
задан Alex 7 October 2011 в 23:39
поделиться