سؤال

I have the following 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>

I am trying to get the package dimensions, but none of my xpath queries work. Have tried these using PHP's built-in xpath functions and using QueryPath 3.0.0, but neither works.

This doesn't work even though XML Tree in Chrome shows it should:

/GetMatchingProductForIdResponse/GetMatchingProductForIdResult/Products/Product/AttributeSets/ns2:ItemAttributes/ns2:PackageDimensions/ns2:Height

I thought neutralizing the namespace would get rid of the problem since the above triggers a namespace error, but it does not:

/GetMatchingProductForIdResponse/GetMatchingProductForIdResult/Products/Product/AttributeSets/*[local-name()='ItemAttributes']/*[local-name()='PackageDimensions']/*[local-name()='Height']
هل كانت مفيدة؟

المحلول 2

I'm afraid even the first part of your xpath is suffering for the namespace issue. Have you tried :

/*[local-name()='GetMatchingProductForIdResponse']/*[local-name()='GetMatchingProductForIdResult']/*[local-name()='Products']/*[local-name()='Product']/*[local-name()='AttributeSets']/*[local-name()='ItemAttributes']/*[local-name()='PackageDimensions']/*[local-name()='Height']

I think to use the normal xpath approach, you will need to set the corresponding namespaces on the xpath evaluator itsself.

نصائح أخرى

Xpath has no default namespace. The Xpath provided by Chrome is invalid. An Xpath element expression without a namespace prefix always matches elements without a namespace.

You need to register your own namespace prefixes and use them:

$dom = new DOMDocument();
$dom->loadXml($xml);

$xpath = new DOMXpath($dom);
$xpath->registerNamespace(
  'p', 'http://mws.amazonservices.com/schema/Products/2011-10-01'
);
$xpath->registerNamespace(
  'pd', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd'
);

$result = $xpath->evaluate(
  'string(
    /p:GetMatchingProductForIdResponse
    /p:GetMatchingProductForIdResult
    /p:Products
    /p:Product
    /p:AttributeSets
    /pd:ItemAttributes
    /pd:PackageDimensions
    /pd:Height
   )'
);

var_dump($result);

Output: https://eval.in/150409

string(4) "3.70"
    /**
     * Convert XML to JSON and return.
     */
    $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
    $xml = simplexml_load_string($response);
    $json = json_encode($xml);
    return json_decode($json,TRUE);

This worked for me

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top