查找所有空 DOM 节点
仅使用 xPath 是否可以识别空 DOM 节点?
例如,任何像这样的节点:
<div></div>
我希望在 xPath 返回的节点上只执行一个 length
,如下所示:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$xpath_rule = "XPATH";
$returned_nodes = $xpath->query($xpath_rule);
if($returned_nodes->length > 0){ // it is not empty
如果这不能用 xPath 完成,我怎样才能有效地做到这一点遍历整个 DOM 树?
PHP5 仅支持 xPath 1.0。
Is it possible to identify a DOM node if it is empty using just xPath?
For example, any node like so:
<div></div>
I am hoping to do just a length
on the returned nodes of the xPath like so:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$xpath_rule = "XPATH";
$returned_nodes = $xpath->query($xpath_rule);
if($returned_nodes->length > 0){ // it is not empty
If this can't be done with xPath, how can I do this efficiently by traversing the whole DOM tree?
Only xPath 1.0 is supported by PHP5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此 XPath 表达式匹配所有没有子元素或仅包含空格的元素:
This XPath expression matches all elements without children or containing only whitespace: