Please forgive if the following is a bit muddled, I've been killing myself trying to work this out.
This is a chunk of XML (exported from a much large site) that I am using to create a category tree for a mini-CMS. Once I've got the value and name of the node, which is no problem, I also need to get the 'parent' each node, that is, the node preceding it which is above it in the hierarchy.
<productCategory>
<genericName>DigitalCinema</genericName>
<productCategories>
<productCategory>
<genericName>DCinemaProj</genericName>
<productModels>
<productModel>ProjProd-1</productModel>
<productModel>ProjProd-2</productModel>
<productModel>ProjProd-3</productModel>
<productModel>ProjProd-4</productModel>
</productModels>
</productCategory>
<productCategory>
<genericName>DCinemaLens</genericName>
</productCategory>
</productCategories>
</productCategory>
For example, for the productCategory-genericName DCinemaLens, I need to be able to grab the parent as DigitalCinema, and similarly for the individual productModel nodes, where the parent would be DCinemaProj.
I've tried various different queries in xpath using ancestor, previous-sibling and parent and I still can't see to grab the node I need.
Here is my code as it stands from giving up on my attempts a few minutes ago.
if ($xml->xpath('//productCategories')) {
foreach($xml->xpath('//genericName | //productModel') as $genericName){
echo "<p align='center'>$genericName";
$type = $genericName->getName();
echo " - (" . $type . ") ";
$derp = $xml->xpath("ancestor::productCategory[1]/genericName");
echo $derp;
echo '</p>';
}
}
I've also had some success getting information in an array, but it always just returns every value in the XML again.
$key = 'genericName';
$derpgleep = $derp[$key];
echo 'Derp= ' . $derpgleep;
print_r($derp);
Hopefully there is a really easy solution I am overlooking. I hope I have been clear.