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

发布于 2024-10-26 07:21:32 字数 2014 浏览 0 评论 0原文

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

Very new to PHP/programming in general, and I've been trying to run a PHP file set up to parse an Atom feed outputted from the Google Search API for Shopping. When run however, the code outputs a large number enter code heref errors (warnings), all having to do with 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

The full code I'm working with can be found here: search.php

Here's the uploaded copy I'm working with that outputs the errors - http://ultra-deals.com/gtest/search.php

And here's the specific code in question

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
 }

Each of those correspond to one or more of the error messages outputted on the search.php file I uploaded to my server. I have no experience with XPath, so I'm not even exactly sure what the namespace prefix in question is. Really appreciate the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-11-02 07:21:32

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

openSearch is the undefined namespace here. You'll have to define that earlier, and everything should start working properly.

阳光下慵懒的猫 2024-11-02 07:21:32

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

You should consider using the XQuery PHP extension to solve your problem: http://www.zorba-xquery.com/site2/html/php.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文