XHTML with DOMImplementation weirdness

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

In short, I want to create XHTML documents. Creating XML objects with
DOMDocument constructor doesn't quite cut it because you need
DOMImplementation to create a DOCTYPE.

Here is how everyone sane would do it:

$doctype = DOMImplementation::createDocumentType(
    ,'-//W3C//DTD XHTML 1.0 Strict//EN'
    ,'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd '
$doc = DOMImplementation::createDocument(null, 'html', $doctype);
$doc->formatOutput = true;
$doc->encoding = 'UTF-8';
$doc->documentElement->setAttribute('lang', 'en');
$head = $doc->createElement('head');
$body = $doc->createElement('body');

OK, so now we have HEAD and BODY inside HTML. So far so good. But
here's the strange part: on output (saveXML() or saveHTML) a META tag
appears inside the HEAD:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Obviously we haven't created this META tag. So who did? Where did it
come from? I didn't want text/html, I want application/xhtml+xml, so I
tried to explicitly override it:

$meta = $this->doc->createElement('meta');
$meta->setAttribute('http-equiv', 'Content-Type');
$meta->setAttribute('content', 'application/xhtml+xml; charset='.$enc);

No use. On every output my META Content-Type tag gets overriden by the
generated META tag.

Manipulating DOM with PHP5 is poorly documented and not much discussed,
which probably means a few people use it. I hope someone can clear out
this oddity for me for it looks like a feature, not a bug.

I wouldn't like to believe that the only solution to change the
content-type is str_replace() after outputting to a string :(


Site Timeline