Topic
No replies
SystemAdmin
SystemAdmin
746 Posts
ACCEPTED ANSWER

Re: Xpath Does Not Correlate Parent With Variable Number Of Children

‏2009-09-22T11:58:25Z  in response to SystemAdmin
It is not clear the working language for the question. I suppose php just for some resemblance. Also, it is not clear exactly how you want the construction of $feed. Since parent being mentioned, I would use a version using precisely parent axis for illustration how it be done. To use xpath, I would use php's SimpleXML extension.

<?php $s = <<<xsource <leagues> <league> <name>NCAA</name> <team> <name>Akron </name> <decimalodds>2</decimalodds> </team> <team> <name>Ohio </name> <decimalodds>1.833333</decimalodds> </team> </league> <league> <name>AFC</name> <team> <name>Indianapolis Colts</name> <decimalodds>1.0625</decimalodds> </team> <team> <name>Jacksonville Jaguars</name> <decimalodds>11</decimalodds> </team> <team> <name>Tennessee Titans</name> <decimalodds>11</decimalodds> </team> </league> </leagues> xsource;   $doc=simplexml_load_string($s); $feed=array(); foreach ($doc->xpath(
'//team') as $child) 
{ foreach ($child->xpath(
'parent::*') as $parent) 
{ $feed[
"$child->name"]=$parent->name; 
} 
} foreach ($feed as $key=>$value) 
{ echo $key.
",".$value.
"\n"; 
} ?>