I might have made a small breakthrough with this. I was trying to control the path through to the leaf nodes using xpath where the attribute splits the path, but with no success.
However I can get this control using level2[0]->level3 etc and can take a count at each of the levels to loop through and find the attribute value so that I know the attribute of the node the children belong to.
Hopefully that will allow me to nail this. Might not be the tidiest solution and your suggestion might prove useful as an option or check, so appreciate you taking the time to respond.
Yes, my shorthand version of the xml should have read as per Colin’s above. The actual example is more complicated but this sums up the challenge I’m struggling with.
In case it might be of interest or use to others - here’s what I ended up with which seems to be working fine
$cat_id="100";
$full_path='xml_generator.php'.'?CategoryID='.$cat_id;
$url=simplexml_load_file($full_path);
//find out the number of aspect nodes
//this is the trunk point where the 'name' attribute can have different values
$noAspectHistograms=count($url->aspectHistogramContainer->aspect);
//check that the category has not already been mapped - external function
$CategoryMapping=isCategoryMapped($cat_id);
//only precede if category is unmapped
if ($CategoryMapping=="unmapped") {
//loop through the Aspect Nodes to find all their children
$i=0;
foreach ($url->aspectHistogramContainer->aspect as $aspect) {
foreach ($aspect->attributes() as $aspectName=>$aspectValue) {
// insert into table1 values for cat_id and aspectValue
// use mysql_insert_id() to get the rowID to use as the aspectID in the second insert query
$aspectID=mysql_insert_id();
}
foreach ($url->aspectHistogramContainer->aspect[$i]->valueHistogram as $nodes) {
foreach ($nodes->attributes() as $HistName=>$HistValue) {
// insert into table2 values for aspect_id and HistValue
}
}
$i++;
}
}