xpath查询获取子元素

时间:2017-03-24 15:42:31

标签: php curl dom xpath

我目前正通过PHP CURL / Xpath使用以下地址来获取一些数据:

 $url: http://v1.syndication.nhschoices.nhs.uk/organisations/hospitals/name/trafford?apikey=TBQBMMKR

使用的代码:

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
 $output = curl_exec($ch);
 curl_close($ch);
 $document = new DOMDocument;
 libxml_use_internal_errors(true);
 $document->loadHTML($output);
 $document->encoding = 'utf-8';
 $xpath = new DOMXPath($document);
 $orgcodes= $xpath->query('//dd[2]');

 $results = array();

 $iCnt = 0; 
 foreach ($orgcodes as $orgcode){ 
 $results[$iCnt]['orgcode'] = $document->saveHTML($orgcode); $iCnt++; }
 $n = 0; 

 for($iCnt = 0, $cnt = count($results); 
 $iCnt < $cnt; $iCnt++){ 
 $n++; 

 echo $results[$iCnt]['orgcode'];

 }?>

我想像这样回应ODS代码:

RM321

RW3TR

但我得到以下内容:

RM321

53.4540634155273

RW3TR

53.4540634155273

有什么想法吗?我猜它是用xquery做的,但我是个谜。

0 个答案:

没有答案
相关问题