Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xpath query

Status
Not open for further replies.

foddski

Programmer
Jul 20, 2008
3
GB
Hi, I need to find the parent attribute of a node, if anyone can help
Code:
<uniquenode>
 <parent att="abc">
   <child>foo</child>
   <child>bar</child>
 </parent>
 <parent att="def">
   <child>foo1</child>
   <child>bar1</child>
 </parent>
</uniquenode>
If child = foo1 parent['att']=def
so far i have
Code:
$word=$xml->xpath("//child");
foreach ($word as $whatever) { 
	if($whatever=='foo1'){....
but this returns every parent of a child ie. all of them, whereas I require just the parent of the node with the content foo1
 
[tt]$result=$xml->xpath("//child[.='foo1']");
while(list( , $node) = each($result)) {
foreach ($node->xpath("parent::*") as $parent) {
echo $parent->getName()."; ";
foreach ($parent->attributes() as $key=>$value) {
echo $key."=".$value."; ";
}
echo "<br />\n";
}
}[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top