2014-05-12 82 views
2

我有下面的XML文档:Xpath查询结果总是空

<?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> 

我试图让封装尺寸,但没有我的XPath查询的工作。已经尝试过使用PHP的内置xpath函数并使用QueryPath 3.0.0,但都无效。

这并不虽然XML树在Chrome的工作表明,它应该:

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

我还以为中和命名空间将摆脱这个问题,因为上述触发一个命名空间的错误,但它不会:

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

回答

1

恐怕即使是xpath的第一部分也会因命名空间问题而受到影响。您是否尝试过:

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

我想用正常的XPath方法,您将需要设置的XPath计算器itsself相应的命名空间。

+0

即得到所期望的字段值。 – eComEvo

+1

我认为使用较短的语法更具可读性: '/ *:GetMatchingProductForIdResponse/*:GetMatchingProductForIdResult/*:Products/*:Product/*:AttributeSets/*:ItemAttributes/*:PackageDimensions/*:Height' – adamretter

+0

根据你的上下文,你也可以考虑一个更简单的不太具体的查询,如:'// *:Product // *:PackageDimensions' – adamretter

2

Xpath没有默认名称空间。 Chrome提供的Xpath无效。没有名称空间前缀的Xpath元素表达式总是匹配没有名称空间的元素。

您需要注册自己的名称空间的前缀,并使用它们:

$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); 

输出:https://eval.in/150409

string(4) "3.70" 
+0

这也适用,谢谢! – eComEvo