PHP DOM。确定元素是否是DOMxpath中另一个元素的后代

时间:2016-06-15 03:20:52

标签: php dom domxpath

如果元素是其他元素的子元素(或孙子元素或者grandgrandchild元素等),如何检查DOM? 我有代码:

$xpath = new DOMXpath($this->dom); #$this->dom is DOMDocument object;
$clones = $xpath->evaluate("(//*[@".$this->cloneAttribute."])"); #get all clones
for($i=0; $i<$clones->length; ++$i) {
    $elems[$i]['path'] = $clones->item($i)->getNodePath();
    $elems[$i]['level'] = count(explode('/', $elems[$i]['path']));
}
print_r($elems);
Array
(
[0] => Array
    (
        [path] => /html/body/section/header/ul/li
        [level] => 7
    )

[1] => Array
    (
        [path] => /html/body/section/aside/div/ul/li[2]
        [level] => 8
    )

[2] => Array
    (
        [path] => /html/body/section/aside/div/ul/li[2]/ul/li
        [level] => 10
    )

[3] => Array
    (
        [path] => /html/body/section/main/section/fieldset
        [level] => 7
    )

[4] => Array
    (
        [path] => /html/body/section/main/section/fieldset/ul/li
        [level] => 9
    )
)

结果我看到第三个元素里面的第四个元素,第一个元素里面的第二个元素。 现在我想从第4和第2个数组中删除,因为我只需要父元素。

0 个答案:

没有答案