Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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

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 - Fatal编程技术网

php将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

我有这样一个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"
    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)