$d = new DOMDocument(); $d->loadXML($xml); $x = new DOMXPath($d); $result = $x->evaluate("//text()[contains(.,'617.99')]/ancestor::*/@id"); $unique = null; for($i = $result->length -1;$i >= 0 && $item = $result->item($i);$i--){ if($x->query("//*[@id='".addslashes($item->value)."']")->length == 1){ echo 'Unique ID is '.$item->value."\n"; $unique = $item->value; break; } } if(is_null($unique)) echo 'no unique ID found';