Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:检查是否存在带有属性的XML节点_Php_Xml_Xpath - Fatal编程技术网

PHP:检查是否存在带有属性的XML节点

PHP:检查是否存在带有属性的XML节点,php,xml,xpath,Php,Xml,Xpath,我似乎无法理解这一点。我有以下XML文件: <?xml version="1.0" encoding="UTF-8"?> <targets> <showcases> <building name="Big Blue" /> <building name="Shiny Red" /> <building name="Mellow Yellow" /> </showcases> <

我似乎无法理解这一点。我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<targets>
  <showcases>
    <building name="Big Blue" />
    <building name="Shiny Red" />
    <building name="Mellow Yellow" />
  </showcases>
</targets>
。。。但是如果我理解正确,那不是只测试第一个
节点吗<代码>项目(0)?我需要为此使用XQuery吗


谢谢你的帮助!谢谢

好吧,看来XPath正是我想要的。下面是我想做的:

<?php

$xmlDocument = new DOMDocument();

$nameToFind = "Shiny Red";

if ($xmlDocument->load('file.xml')) {
        if (checkIfBuildingExists($xmlDocument, $nameToFind)) {
        echo "Found a red building!";
    }
}

function checkIfBuildingExists($xdoc, $name) {
    $result = false;
    $xpath = new DOMXPath($xdoc);
    $nodeList = $xpath->query('/targets/showcases/building', $xdoc);
    foreach ($nodeList as $node) {
        if ($node->getAttribute('name') == $name) {
            $result = true;
        }
    }
    return $result;
}

?>
load('file.xml')){
if(checkIfBuildingExists($xmlDocument,$nameToFind)){
echo“发现了一座红色建筑!”;
}
}
函数checkIfBuildingExists($xdoc,$name){
$result=false;
$xpath=newdomxpath($xdoc);
$nodeList=$xpath->query('/targets/showcases/building',$xdoc);
foreach($nodelistas$node){
如果($node->getAttribute('name')==$name){
$result=true;
}
}
返回$result;
}
?>

此XPath表达式

/*/*/building[@name='闪亮的红色']

选择名为
building
的元素,其
name
属性的值为“闪亮的红色”,并且该元素是顶部元素的子元素的子元素

可能在PHP中有一种计算XPath表达式的方法,然后只计算上面的XPath表达式并使用结果

我建议如下(PHP使用ext/simplexml和XPath):

$name='shinking Red';
$xml=simplexml\u load\u字符串('
');
$nodes=$xml->xpath(sprintf('/targets/showcases/building[@name=“%s”]”,$name);
如果(!空($nodes)){
printf('至少找到一个名为“%s”的建筑,$name);
}否则{
printf('未找到名为“%s”的建筑,$name);
}
如果我理解正确,那不是只测试第一个节点吗

是的。所以,如果你想使用像那样的DOM方法,你必须在循环中完成。例如:

$buildings= $xdoc->getElementsByTagName('building');
foreach ($buildings as $building)
    if ($building->getAttribute('name')==$name)
        return true;
return false;

使用XPath,您可以消除循环,正如Dimitre和sgehrig所说,但是您必须小心允许向XPath表达式中注入哪些字符(例如,$name=“]”“]”将破坏表达式)。

看起来上面的XPath函数缺少一个右括号“)”。否则,感谢您提供的精彩代码
$name = 'Shiny Red';
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?>
<targets>
  <showcases>
    <building name="Big Blue" />
    <building name="Shiny Red" />
    <building name="Mellow Yellow" />
  </showcases>
</targets>');
$nodes = $xml->xpath(sprintf('/targets/showcases/building[@name="%s"]', $name);
if (!empty($nodes)) {
    printf('At least one building named "%s" found', $name);
} else {
    printf('No building named "%s" found', $name);
}
$buildings= $xdoc->getElementsByTagName('building');
foreach ($buildings as $building)
    if ($building->getAttribute('name')==$name)
        return true;
return false;