我在查询包含默认命名空间的xml文件时遇到了问题,这是一场噩梦。
我已经能够在声明命名空间后选择第一个节点,但后面的任何内容都会被忽略。
$str = '<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult>
<Product>
<LowestOfferListings>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Good</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>Unknown</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>83352</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>7.40</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>4.60</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
</LowestOfferListings>
</Product>
</GetLowestOfferListingsForASINResult>
</GetLowestOfferListingsForASINResponse>';
$xml = new SimpleXMLElement($str);
$xml->registerXPathNamespace('c', 'http://mws.amazonservices.com/schema/Products/2011-10-01');
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult")); //works
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult/Product")); //not working
print_r($xml->xpath("c:GetLowestOfferListingsForASINResult//Product")); //not working
答案 0 :(得分:1)
Xpath没有自动默认命名空间(它始终是null-namespace),因此您需要在所有元素名称中明确使用“c:
”前缀:
c:GetLowestOfferListingsForASINResult/c:Product
^^
c:GetLowestOfferListingsForASINResult//c:Product
^^
如果您对详细信息感兴趣,请在此网站上提供以下现有问答: