XPath 查询只能返回最后一个匹配项的原因是什么?我正在对一个显然有多个<a name="...">
标签的 HTML 片段运行查询,但 XPath 查询只会返回一个元素,它恰好是最后一个元素。
function extract($html) {
// This test shows that the retrieved HTML fragment indeed contains multiple anchor tags
echo "<textarea>".$html."</textarea>";
// parse the data
$dom = new DomDocument();
@$dom->loadHTML($html); // we use @$dom to suppress some warnings
$xpath = new DOMXPath($dom);
// find the html code for the post
$query = "//a[contains(@name, 'post')]";
$rows = $xpath->query($query);
// This will return 1
echo "Elements found: " . count($rows);
...
}