php将XML属性与特定属性相加
我有这样一个XML文件:php将XML属性与特定属性相加,php,xml,Php,Xml,我有这样一个XML文件: <devices> <device name="Cabina" altid="e2" id="42" category="3" subcategory="0" room="3" parent="40" status="1" state="-1" comment=""/> <device name="contatto finestra cucina" altid="12" id="28" category="4" subc
<devices>
<device name="Cabina" altid="e2" id="42" category="3"
subcategory="0" room="3" parent="40" status="1" state="-1" comment=""/>
<device name="contatto finestra cucina" altid="12" id="28" category="4"
subcategory="3" room="1" parent="1" armed="0" tripped="1"
lasttrip="1398337234" batterylevel="1" heatsp="19" status="1"/>
<device name="Ingresso" altid="e1" id="15" category="3"
subcategory="0" room="1" parent="14" status="0" state="-1" comment=""/>
</devices>
我想总结属于第3类的设备的状态
在这种情况下,总和=1
谢谢使用XPath表达式: 选择设备节点:
/devices/device
如果类别属性为3:/devices/device[@category=3]
获取状态属性:/devices/device[@category=3]/@status
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('sum(/devices/device[@category = 3]/@status)'));
总结:Sum(/devices/device[@category=3]/@status)
输出:
使用XPath表达式: 选择设备节点:
/devices/device
如果类别属性为3:/devices/device[@category=3]
获取状态属性:/devices/device[@category=3]/@status
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('sum(/devices/device[@category = 3]/@status)'));
总结:Sum(/devices/device[@category=3]/@status)
输出:
float(1)