From:             jazzslider at gmail dot com
Operating system: Windows
PHP version:      5.2.4RC1
PHP Bug Type:     SPL related
Bug description:  SimpleXMLIterator loses ancestry

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

-- 
Edit bug report at http://bugs.php.net/?id=42259&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=42259&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=42259&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=42259&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=42259&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=42259&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=42259&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=42259&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=42259&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=42259&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=42259&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=42259&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=42259&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=42259&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=42259&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=42259&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=42259&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=42259&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=42259&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=42259&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=42259&r=mysqlcfg

Reply via email to