XPath / Domdocument按类名检查子项 [英] XPath/Domdocument check for child by class name
问题描述
我正在尝试在DOMDocument节点循环中按某个类名(具有类名= foo的divs)查找子节点。如果存在,则应将我的foo值设置为1:
I am trying to find child nodes by a certain class name (divs with class name='foo') within a loop of DOMDocument nodes. If it exists it should set my foo value to 1:
我的HTML $ document看起来像:
My HTML $document looks like:
...
<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):
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()可能不是正确的方法,但是我已经尝试了其他方法,但没有找到正确的方法。 :(
getElementsByTagName() might not be the right method for that, but I tried different methods already and don't find the right one. :(
推荐答案
根据您的评论
According to your comment
嗯,是的,但是不幸的是,它仍然无法正常工作。需要知道哪个.posts div具有子元素'foo',因为我需要分析该父元素的内容,并且以后也需要替换它。
Hm yes but still doesn't work unfortunately. Eventually I need to know which .posts div has the child element 'foo' because I need to analyze the content of that parent and also need to replace it later
对上一个答案,您的谓词可能是:
to the previous answer your predicate is probably:
a)选择具有属性class = posts
c)和子元素div
d)的div元素
b)具有属性class = foo
a) select div elements
b) with attribute class=posts
c) and with a child element div
d) which has attribute class=foo
作为xpath表达式:
as xpath expression:
a)// div
b)// div [@ class = posts]
c)// div [@ class = posts和div]
d)// div [@ class =帖子和div [@ class = foo]]
a) //div
b) //div[ @class="posts" ]
c) //div[ @class="posts" and div ]
d) //div[ @class="posts" and div[ @class="foo" ] ]
例如
<?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>
这篇关于XPath / Domdocument按类名检查子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!