Reputation: 2742
I've been having problems querying an xml file that contains default namespaces and it's been a nightmare.
I've been able to select the first node after declaring a namespace but anything that follows gets ignored.
$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
Upvotes: 1
Views: 66
Reputation: 198117
Xpath has no automatic default namespace (it's always the null-namespace), so you need to explicitly use the "c:
" prefix with all element names:
c:GetLowestOfferListingsForASINResult/c:Product
^^
c:GetLowestOfferListingsForASINResult//c:Product
^^
if you're interested in the details why this is the case, there is the following existing Q&A on this website:
Upvotes: 1