note 101804 added to simplexmlelement.xpath

From: Date: Wed, 12 Jan 2011 10:21:58 +0000
Subject: note 101804 added to simplexmlelement.xpath
Groups: php.notes 
Request: Send a blank email to php-notes+get-175659@lists.php.net to get a copy of this message
Xpath actually knows which element is root regardless of element on which you execute it. Example: <?php $string = <<<XML <a> <b> <c>text</c> <c>stuff</c> </b> <b> <c>code</c> </b> </a> XML; header('content-type: text/plain'); $xml = new SimpleXMLElement($string); //relative to root $b0=$xml->b[0]->xpath('//c'); while(list( , $node) = each($b0)) { echo 'b[0]: //c: ',$node,"\n"; } $b1=$xml->b[1]->xpath('//c'); while(list( , $node) = each($b1)) { echo 'b[1]: //c: ',$node,"\n"; } echo "\n"; //relative to current element $b0=$xml->b[0]->xpath('.//c'); while(list( , $node) = each($b0)) { echo 'b[0]: .//c: ',$node,"\n"; } $b1=$xml->b[1]->xpath('.//c'); while(list( , $node) = each($b1)) { echo 'b[1]: .//c: ',$node,"\n"; } ?> Will return: b[0]: //c: text b[0]: //c: stuff b[0]: //c: code b[1]: //c: text b[1]: //c: stuff b[1]: //c: code b[0]: .//c: text b[0]: .//c: stuff b[1]: .//c: code ---- Server IP: 82.177.164.100 Probable Submitter: 79.190.157.69 ---- Manual Page -- http://www.php.net/manual/en/simplexmlelement.xpath.php Edit -- https://master.php.net/note/edit/101804 Del: integrated -- https://master.php.net/note/delete/101804/integrated Del: useless -- https://master.php.net/note/delete/101804/useless Del: bad code -- https://master.php.net/note/delete/101804/bad+code Del: spam -- https://master.php.net/note/delete/101804/spam Del: non-english -- https://master.php.net/note/delete/101804/non-english Del: in docs -- https://master.php.net/note/delete/101804/in+docs Del: other reasons-- https://master.php.net/note/delete/101804 Reject -- https://master.php.net/note/reject/101804 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175659) next »