简体   繁体   中英

Problem with 'undefined namespace prefix' with XPath in PHP?

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM