【问题标题】:Problem with 'undefined namespace prefix' with XPath in PHP?PHP 中 XPath 的“未定义命名空间前缀”有问题吗?
【发布时间】:2011-07-22 01:33:41
【问题描述】:

一般来说,PHP/编程非常新,我一直在尝试运行一个 PHP 文件设置来解析从 Google Search API for Shopping 输出的 Atom 提要。然而,运行时,代码输出大量的输入代码错误(警告),都与 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 的经验,所以我什至不确定有问题的名称空间前缀是什么。真的很感谢帮助!

【问题讨论】:

标签: php xml xpath namespaces prefix


【解决方案1】:

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

【讨论】:

【解决方案2】:

openSearch 是这里未定义的命名空间。您必须更早地定义它,并且一切都应该开始正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多