php - Xpath查询结果始终为空

标签 php xml xpath

我有以下 XML 文档:

<?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,但都不起作用。

尽管 Chrome 中的 XML 树显示它应该起作用,但这不起作用:

/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']

最佳答案

Xpath 没有默认命名空间。 Chrome 提供的 Xpath 无效。不带 namespace 前缀的 Xpath 元素表达式始终匹配不带 namespace 的元素。

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

$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"

关于php - Xpath查询结果始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23619912/

相关文章:

PHP session 超时 htaccess 文件

javascript - PHP 返回 jquery/ajax 不工作

php - 在 zend 框架中的特定列中显示数组数据

java - RecyclerView 中的网格项未正确显示

javascript - 如何在 xsl 文档中使用 javascript 函数?

xml - 这个 xpath 中的 count() 函数是什么?

php - 文件上传请求中的 Content-Transfer-Encoding

xml - 如何使用 XSLT 删除这个棘手的 XML 重复节点?

c++ - appendChild xml c++ 无法按需工作

xml - 匹配具有特定祖先Xpath Xslt的节点