我有这个示例代码,它将提取每个标签的值。除此之外,获取该标签的类名..
<?php
$doc = new DOMDocument;
$doc->loadxml( <<< eox
<tr class="calendar_row" data-eventid="42023">
<td class="date"/>
<td class="time">All Day</td>
<td class="currency">CAD</td>
<td class="impact">
<span title="Non-Economic" class="holiday"/>
</td>
<td class="event">
<span>Bank Holiday</span>
</td>
<td class="detail">
<a class="calendar_detail level1" data-level="1" title="Open Detail"/>
</td>
<td class="actual"/>
<td class="forecast"/>
<td class="previous"/>
<td class="graph"/>
</tr>
eox
);
$xpath = new DOMXPath($doc);
foreach( $xpath->query('//tr[@data-eventid="42023"]/td[@class]') as $n ) {
echo $n->nodeName.'-'.$n->nodeValue."<br />";
}
?>
使用上面的代码段,我想要的只是获取这些值,即使某些标签格式不正确(我正在废弃网络源)。我如何在 DOMDocument XPath 查询中做到这一点。我遇到了麻烦,因为要获取的值是:
td-
td-All Day
td-CAD
td-
td-Bank Holiday
td-
td-
td-
td-
td-
代替:
date-
time-All Day
currency-CAD
impact-
event-Bank Holiday
detail-
actual-
forecast-
previous-
graph-