可能重复:
How to extract a node attribute from XML using PHP’s DOM Parser
我有这样的HTML:
<tr class="calendar_row" data-eventid="48256">
...
</tr>
我只想在网页上选择值或data-eventid
,但我不知道如何在xpath中执行此操作。有可能吗?
答案 0 :(得分:2)
您可以使用@
来获取属性。
//assume $dom is a DOMDocument
$finder = new DOMXPath($dom);
// //tr[@data-eventid] = all tr nodes that have a data-eventid attribute
// /@data-eventid = the attribute node itself as opposed to the tr node
$nodes = $finder->query('//tr[@data-eventid]/@data-eventid');
foreach($nodes as $node) {
echo $node->nodeValue."\n"; // echos your data-eventid value ie. 48256
}