xref: /PHP-5.5/ext/simplexml/tests/bug42259.phpt (revision d35dc78d)
1--TEST--
2Bug #42259 (SimpleXMLIterator loses ancestry)
3--SKIPIF--
4<?php
5if (!extension_loaded('simplexml')) print 'skip';
6if (!extension_loaded("libxml")) print "skip LibXML not present";
7?>
8--FILE--
9<?php
10$xml =<<<EOF
11<xml>
12  <fieldset1>
13    <field1/>
14    <field2/>
15  </fieldset1>
16  <fieldset2>
17    <options>
18      <option1/>
19      <option2/>
20      <option3/>
21    </options>
22    <field1/>
23    <field2/>
24  </fieldset2>
25</xml>
26EOF;
27
28$sxe = new SimpleXMLIterator($xml);
29$rit = new RecursiveIteratorIterator($sxe, RecursiveIteratorIterator::LEAVES_ONLY);
30foreach ($rit as $child) {
31  $path = '';
32  $ancestry = $child->xpath('ancestor-or-self::*');
33  foreach ($ancestry as $ancestor) {
34    $path .= $ancestor->getName() . '/';
35  }
36  $path = substr($path, 0, strlen($path) - 1);
37  echo count($ancestry) . ' steps: ' . $path . PHP_EOL;
38}
39?>
40===DONE===
41--EXPECT--
423 steps: xml/fieldset1/field1
433 steps: xml/fieldset1/field2
444 steps: xml/fieldset2/options/option1
454 steps: xml/fieldset2/options/option2
464 steps: xml/fieldset2/options/option3
473 steps: xml/fieldset2/field1
483 steps: xml/fieldset2/field2
49===DONE===
50