0

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 :)

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jrw89
0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.