的DomDocument。在xml文件中搜索

时间:2016-05-22 20:36:11

标签: php xml xpath

我试图使用DomXPath的方法。但我收到了奇怪的结果:

$xpath = new DomXPath($dom);
$result = $xpath->query('//students/group/student/name["sid"]');
foreach ($result as $node) {
    echo "<", $node->tagName, "> : ", $node->nodeValue, "\n";
       if ( $node->nodeValue == "sid"){
    echo $node->nodeValue;
    break;
}
}

https://ideone.com/AYsVye

为什么过滤条件不起作用?

1 个答案:

答案 0 :(得分:1)

您的xml包含空间arount节点值。要比较它们只需删除那些空格:

if ( trim($node->nodeValue) == "sid") {