Php xpath-检查元素是否多次出现
我正在尝试使用xpath检查元素是否多次出现 XML: 我得到:Php xpath-检查元素是否多次出现,php,xml,xpath,Php,Xml,Xpath,我正在尝试使用xpath检查元素是否多次出现 XML: 我得到: Element='<container id="1"> </container>' Element='<container id="2"> </container>' 如何存档?您不需要数组来检查匹配数。只需count()XPath的结果,即可获得您的号码: $a = '<XML> <containers> <c
Element='<container id="1">
</container>'
Element='<container id="2">
</container>'
如何存档?您不需要数组来检查匹配数。只需
count()
XPath的结果,即可获得您的号码:
$a = '<XML>
<containers>
<container id="1">
</container>
<container id="2">
</container>
</containers>
</XML>';
$xml = simplexml_load_string($a);
$containers = $xml->xpath('/XML/containers/container');
$totalContainers = count($containers); // 2
$a=
';
$xml=simplexml\u load\u字符串($a);
$containers=$xml->xpath('/xml/containers/container');
$totalContainers=计数($containers);//2.
在XPath中-您可以使用
count(/XML/containers/container)
,但不幸的是SimpleXML不支持这种类型的表达式(AFAIK)
Element='<container id="1">
</container>'
Element='<container id="2">
</container>'
$containers = array('container', 'container');
$a = '<XML>
<containers>
<container id="1">
</container>
<container id="2">
</container>
</containers>
</XML>';
$xml = simplexml_load_string($a);
$containers = $xml->xpath('/XML/containers/container');
$totalContainers = count($containers); // 2