在PHP中使用XPath的“未定义的名称空间前缀”问题?

时间:2011-03-24 11:32:16

标签: php xml xpath namespaces prefix

一般来说,PHP /编程都是新手,我一直在尝试运行PHP文件设置来解析从Google Search API for Shopping输出的Atom提要。然而,当运行时,代码输出大量输入代码heref错误(警告),所有都与XPath有关:

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Undefined namespace prefix in /home/ultradea/public_html/gtest/search.php on line 205

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: xmlXPathEval: evaluation failed in /home/ultradea/public_html/gtest/search.php on line 205

Warning: Invalid argument supplied for foreach() in /home6/legionit/public_html/pricemash/search.php on line 289

我正在使用的完整代码可以在这里找到:search.php

以下是我正在使用的上传副本,它会输出错误 - http://ultra-deals.com/gtest/search.php

这是有问题的具体代码

Lines 204-207
$xml = simplexml_load_string($return);
$result = $xml->xpath('//openSearch:totalResults');
$START = array_shift($xml->xpath('openSearch:startIndex'));
$RESULTS = array_shift($xml->xpath('openSearch:totalResults'));
<...> Line 261
$spelling_suggestion = array_shift($xml->xpath('s:spelling/s:suggestion'));
<...> Lines 273-280
$promos = "";
foreach ($xml->xpath('s:promotions/*') as $item) {
   $promos .= "<td><a href='$item[link]'>$item[description]";
   if ($item[imageLink] != "") {
     $promos .= "<br/><img src='$item[imageLink]'>";
   }
   $promos .= "</td>";
}
<...> Lines 328-335
$NARROW = "";
foreach ($xml->xpath('s:facets/*') as $item) {
 $values = $item->xpath('s:bucket');
 $type = $item['type'];
 $attrib_name = $item['name'];
 if ($attrib_name == "") {
   $attrib_name = $item['property'];  // Default types
 }

每个对应于我上传到我的服务器的search.php文件中输出的一个或多个错误消息。我没有使用XPath的经验,所以我甚至不确定所讨论的命名空间前缀是什么。真的很感激帮助!

2 个答案:

答案 0 :(得分:0)

openSearch是未定义的命名空间。你必须提前定义,一切都应该开始正常工作。

答案 1 :(得分:-2)

您应该考虑使用XQuery PHP扩展来解决您的问题:http://www.zorba-xquery.com/site2/html/php.html

相关问题