Answer the question
In order to leave comments, you need to log in
How to properly process XPath result in a loop?
Colleagues, good afternoon.
The task is to parse the xml file in such a way that by setting the values of two elements, we get the value of the third element, which is at the same level as the two known ones.
There is an xml with something like this:
<Model>
<idModel>26016</idModel>
<sModel>DeLorean</sModel>
<idMark>26015</idMark>
</Model>
<Model>
<idModel>2986</idModel>
<sModel>Avenger</sModel>
<idMark>44</idMark>
</Model>
<Model>
<idModel>2976</idModel>
<sModel>Caliber</sModel>
<idMark>44</idMark>
</Model>
<Model>
<idModel>2977</idModel>
<sModel>Caravan</sModel>
<idMark>44</idMark>
</Model>
<Model>
<idModel>3393</idModel>
<sModel>Challenger</sModel>
<idMark>44</idMark>
</Model>
<Model>
<idModel>2978</idModel>
<sModel>Charger</sModel>
<idMark>44</idMark>
</Model>
foreach ($idref->xpath('//Model') as $item) {
if ($item->idMark == $idmark && $item->sModel == $smodel) {
$idmodel = $item->idModel;
break;
}
}
Answer the question
In order to leave comments, you need to log in
I didn’t quite understand what to substitute where and what doesn’t work if you don’t touch anything, but with the help of xpath you can do this://sModel[../idModel='2986' and ../idMark='44']
As a result, the solution of the problem was reduced to one line of code:
echo $idref->xpath('//idModel[../sModel="' . $smodel . '" and ../idMark=' . $idmark . ']')[0];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question