Ok so lets say i have an xml what looks like this example,
<?xml version="1.0" encoding="UTF-8"?>
<stats id="865" name="blaah">
<example name="blaahblaah">
<example1>
<x ppoints="500"/>
</example1>
<example2>
<x ppoints ="150"/>
<x points ="500"/>
<x ppoints ="140"/>
<x points="200"/>
</example2>
</example>
</stats>
what i am trying to do here is to get all the ppoints and round them into one number and all the points and round them into one number,
like so
Stats: “rounded up ppoints” pp & “rounded up points” p
help anyone? many thanks
In this case, as I understand the structure of the document can contain multiple ‘example’ nodes, I think the best approach is to load the XML document and use a XPath expression to get all the nodes with a
ppointsattribute and apointsattribute.Using XPath there are two possible solutions. One using SimpleXMLElement::xpath to retrieve all the required nodes and sum them manually. And another one using DOMXPath::evaluate which allows to evaluate any type of XPath expression and sum the values just using XPath. The latter solutions is simpler.
DOMXPath::evaluate
SimpleXMLElement::xpath