Au lieu de programmer des boucles/conditions compliquées on peut aussi extraire des éléments avec une expression XPath
$dom_object = new DomDocument();
$dom_object->load("student.xml");
// create DOMXPath object with our DOMObject
$xpath = new Domxpath($dom_object) ;
// Get all exercise nodes that are <staf no="14">
// The $result is a DOMNodeList and not just a a simple array.
$result = $xpath->query("//student/exercises/exercise/staf[@no='14']/..") ;
print ("<ul>");
foreach ($result as $exercise) {
print ("<li>");
$title = $xpath->query ("title", $exercise);
print ($title->item(0)->nodeValue . "</li>\n");
}
print ("</ul>");