I’m using xpath to query my xml for “result items that have the name “FT” but I know some result items do not, as it is a live feed. Would I be able to assign these ‘non-objects’ a value so I could input them into my mysql as well as the values returned?
Here are parts of my xml with result items:
<live>
<Match ct="0" id="771597" LastPeriod="2 HF" LeagueCode="19984" LeagueSort="1" LeagueType="LEAGUE" startTime="15:00" status="2 HF" statustype="live" type="2" visible="1">
<Home id="11676" name="Manchester City" standing="1"/>
<Away id="10826" name="Newcastle United" standing="3"/>
<Results>
<Result id="1" name="CURRENT" value="1-1"/>
<Result id="2" name="FT" value="1-1"/>
<Result id="3" name="HT" value="1-0"/>
</Results>
</Match>
<Match ct="0" id="771599" LastPeriod="1 HF" LeagueCode="19984" LeagueSort="1" LeagueType="LEAGUE" startTime="16:00" status="2 HF" statustype="live" type="2" visible="1">
<Home id="11678" name="Norwich City" standing="1"/>
<Away id="10828" name="West Ham United" standing="3"/>
<Results>
<Result id="1" name="Current" value="2-3"/>
<Result id="2" name="HT" value="1-3"/>
</Results>
</Match>
</live>
I’m trying to get all the scores with name ‘FT’ by this bit of php:
$result = $xpath->query('./Results/Result[@name="FT"]', $match);
$halftimescore = $result->item(0)->getAttribute("value");
I’ve changed the above too:
$result = $xpath->query('./Results/Result[@name="FT"]', $match);
if($result->length > 0) {
$halftimescore = $result->item(0)->getAttribute("value");
}
which removes the error but I get the FT values 1-1, 3-0 and for the non-object it repeats 3-0
Your xpath query seems like it’s made inside a loop of
$matches.If there is no FT result for the current
<Match>, it’s normal that$halftimescoreholds the value of the previous one if you didn’t initialize its value at the beginning of your loop.You need to either initialize
$halftimescorefor each<Match>Or use an else statement:
By the way, as there can only be one FT score for a Match, I would use
$result->length === 1to test the xpath result instead.