php / php/php-src

Mixing processing instructions and element nodes gives inconsistent results in SimpleXML

Open
#12,168 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: simplexml Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two PHP reproductions in the issue and inspect the SimpleXML implementation and its existing tests for children() ordering. Compare processing-instruction and element-node handling in both input orders, then add coverage showing the expected order and run the relevant SimpleXML tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.