gpt4 book ai didi

php - Products 和 Product 命名空间破坏了我的 xpath

转载 作者:行者123 更新时间:2023-12-04 16:59:56 27 4
gpt4 key购买 nike

我正在尝试在 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);

}

但到目前为止还行不通。我在这里朝着正确的方向前进吗?

最佳答案

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

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

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

关于php - Products 和 Product 命名空间破坏了我的 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655726/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com