gpt4 book ai didi

xml - Xpath 获取元素值

转载 作者:行者123 更新时间:2023-12-03 17:02:25 25 4
gpt4 key购买 nike

我正在尝试使用 xpath 来获取 Odometer 的值在我的 xml 文件中:

<?xml version="1.0"  encoding="UTF-8"?>
<CWEAI xmlns="http://www.carrierweb.com/Schema/CWEAI_schema_1.0/cwReturnTruckCanbusHistory">

<cwReturnTruckCanbusHistory>
<ID>549094411</ID>
<CWVehicleID>109755</CWVehicleID>
<Odometer>1374031</Odometer>
<Fuel>452800</Fuel>
<FuelLevel>1000</FuelLevel>

</cwReturnTruckCanbusHistory>
</CWEAI>

我已经尝试过任何事情,但我无法获得有效的 xpath:
CWEAI/cwReturnTruckCanbusHistory/Odometer

CWEAI/cwReturnTruckCanbusHistory/Odometer/text

CWEAI[@xmlns="http://www.carrierweb.com/Schema/CWEAI_schema_1.0/cwReturnTruckCanbusHistory"]/cwReturnTruckCanbusHistory/Odometer

最佳答案

text是 Xpath 函数,所以以正确的方式使用它

/CWEAI/cwReturnTruckCanbusHistory/Odometer/text()

结果是 Text='1374031'
在 php 中工作:
$xml = simplexml_load_string($str);
$ns = $xml->getNamespaces(true);
$xml->registerXPathNamespace('c', current($ns));

$target = $xml->xpath('//c:CWEAI//c:cwReturnTruckCanbusHistory/c:Odometer/text()');
echo $target[0];

demo on eval.in

关于xml - Xpath 获取元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43893891/

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