note 89953 modified in function.simplexml-element-xpath by danbrown

From: Date: Wed, 16 Sep 2009 15:50:44 +0000
Subject: note 89953 modified in function.simplexml-element-xpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160674@lists.php.net to get a copy of this message
An other example: <?php $string = <<<XML <a> <b> <c> <d>Apple</d> <d>Banana</d> </c> <e> <f>Kiwi</f> </e> </b> </a> XML; //$string = file_get_contents("test.xml"); $xml = new SimpleXMLElement($string); /* Search for <a><b><c> */ $result = $xml->xpath('/a/b/c/d'); while(list( , $node) = each($result)) { echo '/a/b/c/d: ',$node,"\n"; } /* Relative paths ... : DO NOT WORK */ $result = $xml->xpath('c/d'); while(list( , $node) = each($result)) { echo 'c/d: ',$node,"\n"; } /* Relative paths ... : WORK */ $result = $xml->xpath('b/c/d'); while(list( , $node) = each($result)) { echo 'b/c/d: ',$node,"\n"; } /* Relative paths ... : WORK */ $result = $xml->xpath('//c/d'); while(list( , $node) = each($result)) { echo '//c/d: ',$node,"\n"; } ?> --was-- An other example: <?php $string = <<<XML <a> <b> <c> <d>Apple</d> <d>Banana</d> </c> <e> <f>Kiwi</f> </e> </b> </a> XML; //$string = file_get_contents("test.xml"); $xml = new SimpleXMLElement($string); /* Search for <a><b><c> */ $result = $xml->xpath('/a/b/c/d'); while(list( , $node) = each($result)) { echo '/a/b/c/d: ',$node,"\n"; } /* Relative paths ... : DO NOT WORK */ $result = $xml->xpath('c/d'); while(list( , $node) = each($result)) { echo 'c/d: ',$node,"\n"; } /* Relative paths ... : WORK */ $result = $xml->xpath('b/c/d'); while(list( , $node) = each($result)) { echo 'b/c/d: ',$node,"\n"; } /* Relative paths ... : WORK */ $result = $xml->xpath('//c/d'); while(list( , $node) = each($result)) { echo '//c/d: ',$node,"\n"; } ?> http://php.net/manual/en/function.simplexml-element-xpath.php

« previous php.notes (#160674) next »