Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP查找所有XML命名空间URI_Php_Xml_Xpath - Fatal编程技术网

使用PHP查找所有XML命名空间URI

使用PHP查找所有XML命名空间URI,php,xml,xpath,Php,Xml,Xpath,假设XML文档中有以下名称空间: xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" <?xml version="1.0"?> <GetMatchingProductForIdResponse xmlns="http://mws.amaz

假设XML文档中有以下名称空间:

xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"
<?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>
以下是XML文档:

xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"
<?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>

正在寻找一种更简洁的方法来解决在函数中包装路径的每一部分的需要。

ns2命名空间已注册到products节点,因此需要调用products节点的lookupNamespaceURI方法。这很难看,但却起到了关键作用:

$xpath->registerNamespace('ns', $domDoc->documentElement->lookupNamespaceURI(NULL));
$productNode = $xpath->query("/ns:GetMatchingProductForIdResponse/ns:GetMatchingProductForIdResult/ns:Products")->item(0);
$xpath->registerNamespace('n2', $productNode->lookupNamespaceURI('ns2'));

什么不起作用?您是否已为所有未使用前缀的xpath部分预先添加了
ns:
?是的,例如,以下内容与上面的代码不兼容:
/ns:GetMatchingProductForIdResponse/ns:GetMatchingProductForIdResult/ns:Products/ns:Products/ns:AttributeSet/ns2:ItemAttributes/ns2:PackageDimensions/ns2:Height