|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51534 DOMImplementation::createDocument() does not correctly append children
Submitted: 2010-04-11 19:40 UTC Modified: 2010-04-12 20:50 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: jameswithers89 at googlemail dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3.2 OS: Fedora 12 GNU/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: jameswithers89 at googlemail dot com
New email:
PHP Version: OS:


 [2010-04-11 19:40 UTC] jameswithers89 at googlemail dot com
The product of DOMDocument::createDocument() appends children outside of the qualified name of the document element (i.e. the root element). 

I am using php-5.3.2-1.fc12.i686 and php-xml-5.3.2-1.fc12.i686 installed using Yellowdog Updater, Modified Package Manager. All other DOM methods tried so far work fine.

Test script:
$implementation = new DOMImplementation();
$doctype = $implementation->createDocumentType('html');
$document = $implementation->createDocument('', 'html', $doctype);
$head = $document->createElement('head');
echo $document->saveHTML();

Expected result:
<!DOCTYPE html>
<html xmlns="">

Actual result:
<!DOCTYPE html>
<html xmlns=""></html><head></head>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-11 19:58 UTC] jameswithers89 at googlemail dot com
The following code also produces the same error:
$implementation = new DOMImplementation();
$doctype = $implementation->createDocumentType('html');
// Don't create a root element...
$document = $implementation->createDocument(null, null, $doctype);
// ...instead create a <html> element and set the xmlns attribute:
$html = $document->createElement('html');
$html->setAttribute('xmlns', '');
$head = $document->createElement('head');
echo $document->saveHTML();
 [2010-04-12 16:40 UTC]
-Status: Open +Status: Bogus
 [2010-04-12 16:40 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

It's appending exactly where you are telling it to
 [2010-04-12 20:50 UTC] jameswithers89 at googlemail dot com
At it states "Each 
document contains zero or one doctype nodes, one document element node, and zero 
or more comments or processing instructions; the document element serves as the 
root of the element tree for the document". Correct me if I'm wrong but if that 
is the case then shouldn't the product of:

$document = $implementation->createDocument('', 
'html', $doctype);

use the method DOMNode::appendChild() 'correctly' and place the child element 
within its tags (as a child) rather than next to it (as a sibling)?

In the meantime, the following code gets the desired effect. It does seem a 
rather long way of locating the document/root element and appending a child to 
$implementation = new DOMImplementation();
$doctype = $implementation->createDocumentType('html');
$document = $implementation->createDocument('', 
'html', $doctype);
$head = $document->createElement('head');
$html = $document->getElementsByTagName('html');
$html = $html->item(0);
echo $document->saveXML();
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jul 31 12:01:27 2021 UTC