From: exaton at free dot fr
Operating system: WinXP SP2
PHP version: 5CVS-2006-03-19 (snap)
PHP Bug Type: DOM XML related
Bug description: formatOutput fails when document element has text content
Description:
------------
It appears that DOM output formatting activated with the DOMDocument's
formatOutput property fails as soon as the document's root element ($doc
-> documentElement) contains a text node (even besides other nodes).
Reproduce code:
---------------
function pre_ent_dump($var) {
printf("<pre>\n%s</pre>\n", htmlentities($var));
}
function pre_var_dump($var) {
echo "<pre>\n"; var_dump($var); echo "</pre>\n";
}
$doc = new DOMDocument();
$doc -> load('file.xml');
$doc -> formatOutput = TRUE;
pre_ent_dump($doc -> saveXML()); // (1)
$root = $doc -> documentElement;
pre_var_dump($root -> textContent); // (2)
$child = $doc -> createElement('child');
$root -> appendChild($child);
pre_ent_dump($doc -> saveXML()); // (3)
Actual result:
--------------
[Note : this report is only long because of many examples with slight
variations]
PHP Version 5.1.3RC2-dev (2006-03-19 15:30)
(rolled out specially to confirm the issue, also occurring in 5.1.2)
Apache2 2.0.55, WinXP SP2
The reproduce code shows what we are doing : simply loading the contents
of an XML file, dumping those contents, getting the root element, dumping
the possible text content of itself and its descendants, adding a child to
the root element, and dumping the whole document contents again. Everything
depends, therefore, on the contents of file.xml prior to execution.
When file.xml is :
<?xml version="1.0" standalone="yes"?>
<root></root>
The contents output at (1) and (3) are :
<?xml version="1.0" standalone="yes"?>
<root/>
<?xml version="1.0" standalone="yes"?>
<root>
<child/>
</root>
As expected. But when file.xml is :
<?xml version="1.0" standalone="yes"?>
<root> </root>
Then the contents output at (1) and (3) are :
<?xml version="1.0" standalone="yes"?>
<root> </root>
<?xml version="1.0" standalone="yes"?>
<root> <child/></root>
(1) appears to be as expected, but in (3) the new child is no longer on a
new line and indented. That absence of formatting does not occur with just
any sort of child to the root element however, as we now demonstrate with
the following contents for file.xml :
<?xml version="1.0" standalone="yes"?>
<root><a></a></root>
The contents output at (1) and (3) are :
<?xml version="1.0" standalone="yes"?>
<root>
<a/>
</root>
<?xml version="1.0" standalone="yes"?>
<root>
<a/>
<child/>
</root>
Both as expected. If we add a text node to the root element though (we use
whitespace but this seems to apply to any sequence of characters) :
<?xml version="1.0" standalone="yes"?>
<root> <a></a></root>
Then the contents output at (1) and (3) are :
<?xml version="1.0" standalone="yes"?>
<root> <a/></root>
<?xml version="1.0" standalone="yes"?>
<root> <a/><child/></root>
Now we realize that *both* outputs are broken (the <a /> in (1) should be
on its own line, like above).
Interestingly, the text nodes only cause trouble when they are at the
root. When the contents of file.xml are :
<?xml version="1.0" standalone="yes"?>
<root><a>
</a></root>
Then the contents output at (1) and (3) are :
<?xml version="1.0" standalone="yes"?>
<root>
<a>
</a>
</root>
<?xml version="1.0" standalone="yes"?>
<root>
<a>
</a>
<child/>
</root>
As they should be.
I have tried working around by loading with
loadXML(file_get_contents('file.xml')), by going to and fro with
SimpleXML, by cancelling and setting formatOutput again at different
stages... Obviously the problem is not there.
I do not know if or how this is related to bugs 23726 or 35673 ; it seems
to me I have more details here, but I won't be surprised if you tell me
the problem is at the libxml2 level...
Thanks is advance.
P.S. Side note -- nothing to do here, but the bug report CAPTCHA image
does not seem to work under Firefox 1.5 win32 (works fine in IE), nor does
it accept the CAPTCHA if I get the source, view the image in IE, and type
it in Firefox.
--
Edit bug report at http://bugs.php.net/?id=36790&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=36790&r=trysnapshot44
Try a CVS snapshot (PHP 5.1):
http://bugs.php.net/fix.php?id=36790&r=trysnapshot51
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=36790&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=36790&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=36790&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=36790&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=36790&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=36790&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=36790&r=support
Expected behavior: http://bugs.php.net/fix.php?id=36790&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=36790&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=36790&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=36790&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=36790&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=36790&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=36790&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=36790&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=36790&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=36790&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=36790&r=mysqlcfg