【问题标题】:Products and Product namespace breaks my xpathProducts 和 Product 命名空间破坏了我的 xpath
【发布时间】:2017-10-09 22:10:44
【问题描述】:

我正在尝试在 xml 工作表中查找一些数据。

如果命名空间是这样删除的,我可以从 xml 中获取数据:

    $nodes = $data->xpath('//Products/Product/productId[.='.$sku.']/parent::*');

但是当我加载真正的 xml 时,它的格式如下所示,带有产品的命名空间和产品的命名空间

xml:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><Products xmlns="http://config.services.bol.com/schemas/product-feed-1.1.xsd">
<Product xmlns="http://config.services.bol.com/schemas/product-1.1.xsd"><productId>9200000082000054</productId><ean>8719659945854</ean><title>SULADA Glossy TPU Bescherm Telefoon Hoesje Samsung Galaxy S8 - Rood</title><productUrlNL>https://www.bol.com/nl/p/sulada-glossy-tpu-bescherm-telefoon-hoesje-samsung-galaxy-s8-rood/9200000082000054/</productUrlNL><productUrlBE>https://www.bol.com/be/p/sulada-glossy-tpu-bescherm-telefoon-hoesje-samsung-galaxy-s8-rood/9200000082000054/</productUrlBE><imageUrl>https://s.s-bol.com/imgbase0/imagebase3/large/FC/4/5/0/0/9200000082000054.jpg</imageUrl><mpn>SS800003374</mpn><brand>GEEN</brand><familyName>SULADA Glossy TPU Bescherm Telefoon Hoesje Samsung Galaxy S8 - Rood</familyName><OfferNL><sellingPrice>14.95</sellingPrice><shippingCost>0.00</shippingCost><condition>new</condition><isDeliverable>true</isDeliverable><ultimateOrderTime>23:59:00.000+02:00</ultimateOrderTime><maximalDeliveryDay>12</maximalDeliveryDay><sellerType>P</sellerType></OfferNL><OfferBE><sellingPrice>14.95</sellingPrice><shippingCost>0.00</shippingCost><condition>new</condition><isDeliverable>true</isDeliverable><ultimateOrderTime>23:59:00.000+02:00</ultimateOrderTime><maximalDeliveryDay>12</maximalDeliveryDay><sellerType>P</sellerType></OfferBE><Gpc><segmentName>Communications</segmentName><familyName>Communications</familyName><className>Communicatieaccessoires</className><brickName>Hoesjes voor Mobiele Telefoon</brickName><chunkName>Hoesje voor mobiele telefoon</chunkName></Gpc><Category><unit>Electronics</unit><category>Mobile &amp; Tablets</category><productgroup>Phones Acc</productgroup><productsubgroup>Accessories Mobile</productsubgroup><subsubgroup>Accessories Mobile</subsubgroup></Category><description>&lt;lt/&gt;b&lt;gt/&gt;Let op! Dit hoesje past alleen op de Samsung Galaxy S8 (dus niet op de Samsung Galaxy S8+ en andere toestellen!&lt;lt/&gt;/b&lt;gt/&gt; Bescherm uw Samsung Galaxy S8 met dit geweldige telefoonhoesje. Dit hoesje is precies op maat gemaakt voor de Samsung Galaxy S8 waardoor het mooi aansluit. Het materiaal waaruit dit hoesje is vervaardigd is van zeer hoge kwaliteit. Met de aanschaf van dit duurzame telefoonhoesje zal uw smartphone gegarandeerd langer meegaan.</description><Properties><Property><key>AppearanceName</key><value>Physical</value></Property><Property><key>Colour</key><value>Meerkleurig</value></Property><Property><key>DeliveryMethod</key><value>Parcel</value></Property><Property><key>Material</key><value>Polycarbonaat</value></Property></Properties></Product>
<Product xmlns="http://config.services.bol.com/schemas/product-1.1.xsd"><productId>9200000074000054</productId><ean>7101125954841</ean><title>Adapter Samsung Galaxy J710 - J7 2016 Origineel</title><productUrlNL>https://www.bol.com/nl/p/adapter-samsung-galaxy-j710-j7-2016-origineel/9200000074000054/</productUrlNL><productUrlBE>https://www.bol.com/be/p/adapter-samsung-galaxy-j710-j7-2016-origineel/9200000074000054/</productUrlBE><imageUrl>https://s.s-bol.com/imgbase0/imagebase3/large/FC/4/5/0/0/9200000074000054.jpg</imageUrl><brand>Merkloos</brand><OfferNL><sellingPrice>12.95</sellingPrice><shippingCost>0.00</shippingCost><condition>new</condition><isDeliverable>true</isDeliverable><ultimateOrderTime>18:00:00.000+02:00</ultimateOrderTime><maximalDeliveryDay>1</maximalDeliveryDay><sellerType>P</sellerType></OfferNL><Gpc><segmentName>Electrical Supplies</segmentName><familyName>Electrical Connection/Distribution</familyName><className>Batterijen en opladers</className><brickName>Opladers</brickName><chunkName>Oplader voor mobiele telefoon</chunkName></Gpc><Category><unit>Electronics</unit><category>Mobile &amp; Tablets</category><productgroup>Phones Acc</productgroup><productsubgroup>Accessories Mobile</productsubgroup><subsubgroup>Accessories Mobile</subsubgroup></Category><description>Productomschrijving Adapter Samsung Galaxy J710 - J7 2016 Origineel  Heeft u behoefte aan een reserveoplader of kunt u uw huidige Samsung oplader niet meer vinden? Dan biedt deze 100% kwalitatieve Samsung oplader uitkomst! Haal ?m vandaag nog in huis!  De Samsung adapter bevat tal van voordelen. Zo is de adapter voorzien van een EU Plug en zorgen de minimale afmetingen ervoor dat deze Samsung adapter ideaal is om bij u te dragen. Schaf nu een originele Samsung adapter met de juiste datakabel aan en ben verzekerd van een optimale oplaadkwaliteit dankzij de capaciteit van 2 Ampère.  Kenmerken originele Samsung adapter  Deze adapter wordt geleverd zonder datakabel en heeft de volgende specificaties:Merk: Samsung Capaciteit: 2 Ampère Type: ETA-U90EWEG Contact: EU PlugAansluiting: USB 2.0</description><Properties><Property><key>AppearanceName</key><value>Physical</value></Property><Property><key>DeliveryMethod</key><value>Parcel</value></Property></Properties></Product></Products>

所以现在有了命名空间,我的 xpath 不再工作了。我尝试注册命名空间并将我的 xpath 函数更改为此(使用我找到的一些示例代码):

php:

function findit($sku,$xml){
$xml->registerNamespace('prodfeed', 'http://config.services.bol.com/schemas/product-feed-1.1.xsd');
$xml->registerNamespace('prod', 'http://config.services.bol.com/schemas/product-1.1.xsd');

$nodes = $xml->xpath('/prodfeed:Products/prodfeed:Product/prod:productId[.='.$sku.']/parent::*');

var_dump($nodes);

}

但到目前为止它还没有工作。我的方向正确吗?

但是

【问题讨论】:

    标签: php xml xpath namespaces


    【解决方案1】:

    XML 文档在到达 Product 元素时会更改默认命名空间,因此此时您需要开始使用 prod 命名空间而不是 prodfeed 命名空间。

    您还可以通过将开头的“[”移回 productId 之前来简化查询,这意味着您不必执行 parent::* 位。

    $nodes = $xml->xpath('/prodfeed:Products/prod:Product[prod:productId=' . $sku . ']');
    

    【讨论】:

      【解决方案2】:

      您正在注册 2 个命名空间 prodfeedprod,但在您的 xpath 中您只使用了“prodfeed”。

      如果您可以更改此行,也许会有所帮助:

      $nodes = $xml->xpath('/prodfeed:Products/prodfeed:Product/prod:productId[.='.$sku.']/parent::*');
      

      到这里:

      $nodes = $xml->xpath('/prodfeed:Products/prod:Product/prod:productId[.=' . $sku . ']/parent::*');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 2014-04-19
        • 1970-01-01
        相关资源
        最近更新 更多