xref: /PHP-8.0/ext/simplexml/tests/bug42259.phpt (revision a555cc0b)
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--EXPECT--
413 steps: xml/fieldset1/field1
423 steps: xml/fieldset1/field2
434 steps: xml/fieldset2/options/option1
444 steps: xml/fieldset2/options/option2
454 steps: xml/fieldset2/options/option3
463 steps: xml/fieldset2/field1
473 steps: xml/fieldset2/field2
48