In short, I want to create XHTML documents. Creating XML objects with
DOMDocument constructor doesn't quite cut it because you need
DOMImplementati on to create a DOCTYPE.
Here is how everyone sane would do it:
$doctype = DOMImplementati on::createDocum entType(
'html'
,'-//W3C//DTD XHTML 1.0 Strict//EN'
,'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
);
$doc = DOMImplementati on::createDocum ent(null, 'html', $doctype);
$doc->formatOutput = true;
$doc->encoding = 'UTF-8';
$doc->documentElemen t->setAttribute(' lang', 'en');
$head = $doc->createElement( 'head');
$body = $doc->createElement( 'body');
$doc->documentElemen t->appendChild($h ead);
$doc->documentElemen t->appendChild($b ody);
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) ;
$head->appendChild($m eta);
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 :(
--
Mislav
DOMDocument constructor doesn't quite cut it because you need
DOMImplementati on to create a DOCTYPE.
Here is how everyone sane would do it:
$doctype = DOMImplementati on::createDocum entType(
'html'
,'-//W3C//DTD XHTML 1.0 Strict//EN'
,'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
);
$doc = DOMImplementati on::createDocum ent(null, 'html', $doctype);
$doc->formatOutput = true;
$doc->encoding = 'UTF-8';
$doc->documentElemen t->setAttribute(' lang', 'en');
$head = $doc->createElement( 'head');
$body = $doc->createElement( 'body');
$doc->documentElemen t->appendChild($h ead);
$doc->documentElemen t->appendChild($b ody);
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) ;
$head->appendChild($m eta);
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 :(
--
Mislav