php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42259 SimpleXMLIterator loses ancestry
Submitted: 2007-08-09 18:09 UTC Modified: 2007-08-14 12:13 UTC
From: jazzslider at gmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 5.2.4RC1 OS: Windows
Private report: No CVE-ID: None
 [2007-08-09 18:09 UTC] jazzslider at gmail dot com
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-14 12:13 UTC] rrichards@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC