XPath / Domdocument按类名检查子项

时间:2012-02-20 13:46:11

标签: php xpath domdocument

我试图在DOMDocument节点的循环中通过某个类名(divs with class name ='foo')查找子节点。如果它存在,它应该将我的foo值设置为1:

我的HTML $文档如下:

...
<div class="posts">Div Posts 1</div>
<div class="posts">Div Posts 2<div class="foo"></div></div>
<div class="posts">Div Posts 3</div>
<div class="posts">Div Posts 4<div class="foo"></div></div>
<div class="posts">Div Posts 5</div>
...

DOMDocument / Xpath($ document):

$html = array();
$document = new \DOMDocument();
$document->loadHTMLFile($url); // loads html from above
$xpath = new \DOMXPath($document);

$i=0;
foreach ($xpath->query(Parser::cssToXpath('.posts')) as $node) {
    $html['posts'][$i]['content'] = $node->nodeValue;  
    // check if child node with class name 'foo' exists => doesn't work :(
    $children = $node->getElementsByTagName('foo');
    if($children)
        $html['posts'][$i]['foo'] = '1';
    else
        $html['posts'][$i]['foo'] = '0';
    $i++;
}

输出:

[posts] => Array
    (
        [0] => Array
            (
                [content] => Div class Posts 1
                [foo] => 1
            )

        [1] => Array
            (
                [content] => Div class Posts 2
                [foo] => 1
            )

        [2] => Array
            (
                [content] => Div class Posts 3
                [foo] => 1
            )

        [3] => Array
            (
                [content] => Div class Posts 4
                [foo] => 1
            )

        [4] => Array
            (
                [content] => Div class Posts 5
                [foo] => 1
            )

    )

getElementsByTagName()可能不是正确的方法,但我已经尝试了不同的方法,但找不到合适的方法。 :(

2 个答案:

答案 0 :(得分:2)

根据你的评论

是的,但遗憾的是仍然无效。最后我需要知道哪个.posts div有子元素'foo',因为我需要分析该父元素的内容,并且还需要稍后将其替换
到你的谓词可能的上一个答案:

< / p>

a)选择div元素
b)属性class = posts
c)和子元素div d)具有属性class = foo

作为xpath表达式:

a)// div b)// div [@ class =“posts”]
c)// div [@ class =“posts”和div]
d)// div [@ class =“posts”和div [@ class =“foo”]]

e.g。

<?php
$doc = new DOMDocument;
$doc->loadhtml( getData() );
$xpath = new DOMXPath($doc);   

/*
use something like
    //div[contains(concat(' ',normalize-space(@class),' '),' post ')]
if the html element may have class="post lalala"
*/
foreach( $xpath->query('//div[@class="posts" and div[@class="foo"]]') as $post) {
    while ( $post->firstChild ) {
        $post->removeChild( $post->firstChild );
    }   
    $post->appendChild( $doc->createElement('span', 'The quick fox....') );
}
echo $doc->savehtml();


function getData() {
    return <<< eoh
<html><head><title>...</title></head><body>
    <div class="posts">Div Posts 1</div>
    <div class="posts">Div Posts 2<div class="foo"></div></div>
    <div class="posts">Div Posts 3</div>
    <div class="posts">Div Posts 4<div class="foo"></div></div>
    <div class="posts">Div Posts 5</div>
</body></html>
eoh;
}

打印

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>...</title></head><body>
    <div class="posts">Div Posts 1</div>
    <div class="posts"><span>The quick fox....</span></div>
    <div class="posts">Div Posts 3</div>
    <div class="posts"><span>The quick fox....</span></div>
    <div class="posts">Div Posts 5</div>
</body></html>

答案 1 :(得分:1)

使用此XPath查找类div

的所有foo元素
//div[@class = 'foo']

更具体地说,请使用:

//div[@class = 'posts']/div[@class = 'foo']