我似乎无法想出这个.我有以下
XML文件:
<?xml version="1.0" encoding="UTF-8"?> <targets> <showcases> <building name="Big Blue" /> <building name="Shiny Red" /> <building name="Mellow Yellow" /> </showcases> </targets>
我需要能够测试是否< building>节点以给定名称存在.我似乎在Google上找到的所有内容都告诉我要执行以下操作:
$xdoc->getElementsByTagName('building')->item(0)->getAttributeNode('name')
…但如果我理解正确,那不是只测试第一个< building>节点?项目(0)?我需要使用XQuery吗?
我很感激一些帮助!谢谢!
我建议如下(PHP使用ext / simplexml和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); }