|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-08-14 12:13 UTC] rrichards@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 08:00:01 2025 UTC |
Description: ------------ I'm getting this error on PHP 5.2.2 and haven't had a chance to upgrade to 5.2.4 yet, but I wanted to mention it anyway since it seems like it would be sort of a niche bug. When iterating over XML using RecursiveIteratorIterator(SimpleXMLIterator(xml)), XPath expressions intended to retrieve a node's ancestors do not retrieve all of them. In the below example, this means that only the immediate parent of the current node can be identified; in other test cases I've run, not even the parent was available. Reproduce code: --------------- $xml = '<xml><fieldset1><field1/><field2/></fieldset1><fieldset2><options><option1/><option2/><option3/></options><field1/><field2/></fieldset2></xml>'; $sxe = new SimpleXMLIterator($xml); $rit = new RecursiveIteratorIterator($sxe, RecursiveIteratorIterator::LEAVES_ONLY); foreach ($rit as $child) { $path = ''; $ancestry = $child->xpath('ancestor-or-self::*'); foreach ($ancestry as $ancestor) { $path .= $ancestor->getName() . '/'; } $path = substr($path, 0, strlen($path) - 1); echo count($ancestry) . ' steps: ' . $path . PHP_EOL; } Expected result: ---------------- 3 steps: xml/fieldset1/field1 3 steps: xml/fieldset1/field2 1 steps: xml/fieldset2/options/option1 1 steps: xml/fieldset2/options/option2 1 steps: xml/fieldset2/options/option3 3 steps: xml/fieldset2/field1 3 steps: xml/fieldset2/field2 Actual result: -------------- 2 steps: fieldset1/field1 2 steps: fieldset1/field2 2 steps: options/option1 2 steps: options/option2 2 steps: options/option3 2 steps: fieldset2/field1 2 steps: fieldset2/field2