【发布时间】:2015-06-22 05:31:08
【问题描述】:
我期待Amount 的数组,但由于某种原因它拒绝被发现。我尝试了许多设置名称空间的组合,但它给出了错误SimpleXMLElement::xpath(): Undefined namespace prefix。
Code
// $xml->registerXPathNamespace("amazon", "http://webservices.amazon.com/AWSECommerceService/2011-08-01");
// $item->registerXPathNamespace("amazon", "http://webservices.amazon.com/AWSECommerceService/2011-08-01");
// $possiblePrices = $item->OfferSummary->xpath('//amazon:Amount');
$possiblePrices = $item->OfferSummary->xpath('//Amount');
Yii::error(print_r($item->OfferSummary->asXML(), true));
Yii::error(print_r($possiblePrices, true));
Log
2015-04-15 21:46:06 [::1][-][-][error][application] <OfferSummary><LowestNewPrice><Amount>1</Amount><CurrencyCode>USD</CurrencyCode><FormattedPrice>$0.01</FormattedPrice></LowestNewPrice><TotalNew>15</TotalNew><TotalUsed>0</TotalUsed><TotalCollectible>0</TotalCollectible><TotalRefurbished>0</TotalRefurbished></OfferSummary>
in /cygdrive/c/Users/Chloe/workspace/xxx/controllers/ShoppingController.php:208
in /cygdrive/c/Users/Chloe/workspace/xxx/controllers/ShoppingController.php:106
2015-04-15 21:46:06 [::1][-][-][error][application] Array
(
)
Top of XML
<?xml version="1.0" ?>
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
【问题讨论】:
-
在致电
asXML()之前,您能否发布更完整的原始 XML 示例 -
这里是完整的 XML pastebin.com/U5ar3UQh
-
这似乎解决了您正在处理的默认命名空间(没有元素前缀)问题:stackoverflow.com/questions/21143846/…
-
@MichaelBerkowski 好吧,我确实尝试过使用命名空间。请参见代码的第 1、2、3 行。但是他们给出了一个错误。
-
对PHP不熟悉,但是既然你把命名空间注册到
$item,也许这样就可以了:$item->xpath('//amazon:OfferSummary//amazon:Amount');?