如何进行数组 xpath 查询?
how to make an array xpath queries?
如何使用 xpath 创建这样的数组????请帮我
我目前正在尝试从 SIMPLEXML/XPATH 查询生成的数组中提取值。我的尝试没有成功,如果有人能看一下,我将不胜感激。
<ol class="font_8 timeline-delivered">
<li class="timelineDate">28 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
<div class="C">CC</div>
</li>
<li class="timelineDate">29 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
</li>
<li class="timelineDate">30 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
<div class="C">CC</div>
</li>
<li class="timelineDate">31 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
</li>
</ol>
在 php 中是否有其他人有过此行为和相关 xpath 查询的经验?
$tabledata = $xpath->query('//ol/li[contains(@class, "timelineDate") or contains(@class, "Timeline-even")]');
foreach($tabledata as $row => $tr) {
foreach($tr->childNodes as $td) {
foreach($td->childNodes as $td) {
$ShipmentDetail[$row][] = preg_replace('~[\r\n]+~', '', trim($td->textContent));
}
}
$ShipmentDetail[$row] = array_values(array_filter($ShipmentDetail[$row]));
}
echo '<pre>';
print_r($ShipmentDetail);
结果数组
Array
(
[0] => Array
(
[0] => 28 Aug 2021
)
[1] => Array
(
[0] => AA
[1] => BB
[2] => CC
)
[2] => Array
(
[0] => 29 Aug 2021
)
[3] => Array
(
[0] => AA
[1] => BB
)
[4] => Array
(
[0] => 30 Aug 2021
)
[5] => Array
(
[0] => AA
[1] => BB
[2] => CC
)
[6] => Array
(
[0] => 31 Aug 2021
)
[7] => Array
(
[0] => AA
)
)
我想成为这样的人
Array
(
[0] => Array
(
[0] => 28 Aug 2021
[1] => AA
[2] => BB
[3] => CC
)
[1] => Array
(
[0] => 29 Aug 2021
[1] => AA
[2] => BB
)
[2] => Array
(
[0] => 30 Aug 2021
[1] => AA
[2] => BB
[3] => CC
)
[3] => Array
(
[0] => 31 Aug 2021
[1] => AA
)
)
这将return与您想要的结果完全相同:
$ShipmentDetail = array();
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$xpath = new DOMXPath($DOM);
$event_nodes = $xpath->query('//li[@class="Timeline-event"]');
foreach ($event_nodes as $event_node) {
$event = array();
$event_date = $xpath->evaluate('string(./preceding-sibling::li[1]/text())', $event_node);
$event[] = $event_date;
$details = $xpath->query('./div', $event_node);
foreach ($details as $detail_node) {
$detail = $xpath->evaluate('string(./text())', $detail_node);
$event[] = $detail;
}
$ShipmentDetail[] = $event;
}
如何使用 xpath 创建这样的数组????请帮我 我目前正在尝试从 SIMPLEXML/XPATH 查询生成的数组中提取值。我的尝试没有成功,如果有人能看一下,我将不胜感激。
<ol class="font_8 timeline-delivered">
<li class="timelineDate">28 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
<div class="C">CC</div>
</li>
<li class="timelineDate">29 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
</li>
<li class="timelineDate">30 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
<div class="b">BB</div>
<div class="C">CC</div>
</li>
<li class="timelineDate">31 Aug 2021</li>
<li class="Timeline-event">
<div class="A">AA</div>
</li>
</ol>
在 php 中是否有其他人有过此行为和相关 xpath 查询的经验?
$tabledata = $xpath->query('//ol/li[contains(@class, "timelineDate") or contains(@class, "Timeline-even")]');
foreach($tabledata as $row => $tr) {
foreach($tr->childNodes as $td) {
foreach($td->childNodes as $td) {
$ShipmentDetail[$row][] = preg_replace('~[\r\n]+~', '', trim($td->textContent));
}
}
$ShipmentDetail[$row] = array_values(array_filter($ShipmentDetail[$row]));
}
echo '<pre>';
print_r($ShipmentDetail);
结果数组
Array
(
[0] => Array
(
[0] => 28 Aug 2021
)
[1] => Array
(
[0] => AA
[1] => BB
[2] => CC
)
[2] => Array
(
[0] => 29 Aug 2021
)
[3] => Array
(
[0] => AA
[1] => BB
)
[4] => Array
(
[0] => 30 Aug 2021
)
[5] => Array
(
[0] => AA
[1] => BB
[2] => CC
)
[6] => Array
(
[0] => 31 Aug 2021
)
[7] => Array
(
[0] => AA
)
)
我想成为这样的人
Array
(
[0] => Array
(
[0] => 28 Aug 2021
[1] => AA
[2] => BB
[3] => CC
)
[1] => Array
(
[0] => 29 Aug 2021
[1] => AA
[2] => BB
)
[2] => Array
(
[0] => 30 Aug 2021
[1] => AA
[2] => BB
[3] => CC
)
[3] => Array
(
[0] => 31 Aug 2021
[1] => AA
)
)
这将return与您想要的结果完全相同:
$ShipmentDetail = array();
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$xpath = new DOMXPath($DOM);
$event_nodes = $xpath->query('//li[@class="Timeline-event"]');
foreach ($event_nodes as $event_node) {
$event = array();
$event_date = $xpath->evaluate('string(./preceding-sibling::li[1]/text())', $event_node);
$event[] = $event_date;
$details = $xpath->query('./div', $event_node);
foreach ($details as $detail_node) {
$detail = $xpath->evaluate('string(./text())', $detail_node);
$event[] = $detail;
}
$ShipmentDetail[] = $event;
}