Question

Suppose I have the following namespaces in an XML document:

xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"

Here is the XML document:

<?xml version="1.0"?>
<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductForIdResult Id="B0009VCOU4" IdType="ASIN" status="Success">
    <Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Product>
        <Identifiers>
            <MarketplaceASIN>
                <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
                <ASIN>B0009VCOU4</ASIN>
            </MarketplaceASIN>
        </Identifiers>
        <AttributeSets>
            <ns2:ItemAttributes xml:lang="en-US">
                <ns2:Binding>Electronics</ns2:Binding>
                <ns2:Brand>DOBANI</ns2:Brand>
                <ns2:Feature>Handcrafted Quality, Value Priced</ns2:Feature>
                <ns2:Feature>Satisfaction Guaranteed! 30-Day Return Policy!</ns2:Feature>
                <ns2:ItemDimensions>
                    <ns2:Height Units="inches">7.00</ns2:Height>
                    <ns2:Length Units="inches">6.00</ns2:Length>
                    <ns2:Width Units="inches">6.00</ns2:Width>
                </ns2:ItemDimensions>
                <ns2:Label>Mid-East</ns2:Label>
                <ns2:ListPrice>
                    <ns2:Amount>9.90</ns2:Amount>
                    <ns2:CurrencyCode>USD</ns2:CurrencyCode>
                </ns2:ListPrice>
                <ns2:Manufacturer>Mid-East</ns2:Manufacturer>
                <ns2:Model>BULB</ns2:Model>
                <ns2:PackageDimensions>
                    <ns2:Height Units="inches">3.70</ns2:Height>
                    <ns2:Length Units="inches">8.10</ns2:Length>
                    <ns2:Width Units="inches">4.00</ns2:Width>
                    <ns2:Weight Units="pounds">0.35</ns2:Weight>
                </ns2:PackageDimensions>
                <ns2:PackageQuantity>1</ns2:PackageQuantity>
                <ns2:PartNumber>BULB</ns2:PartNumber>
                <ns2:ProductGroup>Single Detail Page Misc</ns2:ProductGroup>
                <ns2:ProductTypeName>MUSICAL_INSTRUMENTS</ns2:ProductTypeName>
                <ns2:Publisher>Mid-East</ns2:Publisher>
                <ns2:SmallImage>
                    <ns2:URL>http://ecx.images-amazon.com/images/I/31Fsu5jKWsL._SL75_.jpg</ns2:URL>
                    <ns2:Height Units="pixels">75</ns2:Height>
                    <ns2:Width Units="pixels">50</ns2:Width>
                </ns2:SmallImage>
                <ns2:Studio>Mid-East</ns2:Studio>
                <ns2:Title>Spare Rubber Bulb</ns2:Title>
            </ns2:ItemAttributes>
        </AttributeSets>
        <Relationships/>
        <SalesRankings>
            <SalesRank>
                <ProductCategoryId>sdp_misc_display_on_website</ProductCategoryId>
                <Rank>36468</Rank>
            </SalesRank>
        </SalesRankings>
    </Product>
</Products>
</GetMatchingProductForIdResult>
<ResponseMetadata>
    <RequestId>afnapq823haeufabq2rhalhtz</RequestId>
</ResponseMetadata>
</GetMatchingProductForIdResponse>

How would I lookup both of these so they can be registered for Xpath usage?

Originally, I thought the following would work but it does not:

$domDoc =  new DOMDocument();

$domDoc->loadXML($xml);

$xpath = new DOMXPath($domDoc);

$xpath->registerNamespace('ns', $domDoc->documentElement->lookupNamespaceURI(NULL));
$xpath->registerNamespace('n2', $domDoc->documentElement->lookupNamespaceURI('ns2'));

Looking for a cleaner way around the need to wrap every part of the path in the local-name function.

Était-ce utile?

La solution

The ns2 namespace is registered to the products node, so you need to call the products node's lookupNamespaceURI method. This is ugly but it does the trick:

$xpath->registerNamespace('ns', $domDoc->documentElement->lookupNamespaceURI(NULL));
$productNode = $xpath->query("/ns:GetMatchingProductForIdResponse/ns:GetMatchingProductForIdResult/ns:Products")->item(0);
$xpath->registerNamespace('n2', $productNode->lookupNamespaceURI('ns2'));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top