Skip to content

Mixing processing instructions and element nodes gives inconsistent results in SimpleXML #12168

Open

Description

@ndossche

Description

The following code:

<?php

$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<container>
    <x/><?hello world?>
</container>
XML;

$sxe = simplexml_load_string($xml);

var_dump($sxe->children());

// x and hello swapped
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<container>
    <?hello world?><x/>
</container>
XML;

$sxe = simplexml_load_string($xml);

var_dump($sxe->children());

Resulted in this output:

object(SimpleXMLElement)#2 (2) {
  ["x"]=>
  object(SimpleXMLElement)#4 (0) {
  }
  ["hello"]=>
  object(SimpleXMLElement)#5 (0) {
  }
}
object(SimpleXMLElement)#1 (1) {
  ["x"]=>
  object(SimpleXMLElement)#5 (0) {
  }
}

But I expected this output instead:

object(SimpleXMLElement)#2 (2) {
  ["x"]=>
  object(SimpleXMLElement)#4 (0) {
  }
  ["hello"]=>
  object(SimpleXMLElement)#5 (0) {
  }
}
object(SimpleXMLElement)#1 (1) {
  ["hello"]=>
  object(SimpleXMLElement)#%d (0) {
  }
  ["x"]=>
  object(SimpleXMLElement)#%d (0) {
  }
}

PHP Version

PHP 8.1+

Operating System

Linux

Activity

  1. ndossche commented on Sep 10, 2023

    @ndossche
    MemberAuthor

    This is because php_sxe_iterator_fetch lets the iterator point at the first element node. That's right for SXE_ITER_ELEMENT but not for SXE_ITER_NONE, especially considering that forwards iterating from that point does not skip PIs.

    EDIT: and so, this problem also occurs with comments... (https://bugs.php.net/bug.php?id=43542 looks somewhat related in that it points out a similar inconsistency wrt iterators & nodes)

  2. Truta446 commented on Oct 27, 2026

    @Truta446

    I opened #24791 for this. For children(), the property table now starts at the first child instead of the first element child, so leading PIs and comments show up the same way trailing ones already did (matching (array) of the element itself). Iteration is unchanged.

  3. added a commit that references this issue on Oct 27, 2026
    1428f37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions