I have the following code:

<?php
$implementation = new DOMImplementation();
$document = $implementation->createDocument('http://www.w3.org/1999/xhtml', 'html', $implementation->createDocumentType('html'));
echo $document->saveHTML();
?>

which returns:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"></html>

...exactly as I wanted it to. However, when I run the following:

<?php
$implementation = new DOMImplementation();
$document = $implementation->createDocument('http://www.w3.org/1999/xhtml', 'html', $implementation->createDocumentType('html'));
$head = $document->createElement('head');
$document->appendChild($head);
echo $document->saveHTML();
?>

I get:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"></html><head></head>

...when I was trying to get:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head></head></html>

Does anyone know where I've gone wrong? It appears I might have missed something in the Manual whilst searching for a solution.

Thanks in advance for any help/tips/pointers :)

I have the following code:

<?php
$implementation = new DOMImplementation();
$document = $implementation->createDocument('http://www.w3.org/1999/xhtml', 'html', $implementation->createDocumentType('html'));
echo $document->saveHTML();
?>

which returns:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"></html>

...exactly as I wanted it to. However, when I run the following:

<?php
$implementation = new DOMImplementation();
$document = $implementation->createDocument('http://www.w3.org/1999/xhtml', 'html', $implementation->createDocumentType('html'));
$head = $document->createElement('head');
$document->appendChild($head);
echo $document->saveHTML();
?>

I get:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"></html><head></head>

...when I was trying to get:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head></head></html>

Does anyone know where I've gone wrong? It appears I might have missed something in the Manual whilst searching for a solution.

Thanks in advance for any help/tips/pointers :)

If anyone comes across this post in the future, the following is a working solution (I cannot be sure but I think the above error is down to me misunderstanding the output of createDocument more than anything.):

$implementation = new DOMImplementation();
$document = $implementation->createDocument(null, null, $implementation->createDocumentType('html'));
$html = $document->createElement('html');
$html->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$document->appendChild($html);
$head = $document->createElement('head');
$html->appendChild($head);
echo $document->saveHTML();

...which nicely outputs:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head></head></html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.