Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOM: change doctype of existing DOMDocument

When creating a DOMDocument with DOMImplementation::createDocument(), you can specify a doctype as the third argument in the constructor. This doctype then gets "tied" to the document and you can retrieve it later with $document->doctype.

However, this is a read-only attribute (unlike encoding and version!).

Is there any way to change the doctype property of an existing DOMDocument object?

The only workaround I can think of is to create a new DOMDocument with the new doctype, and import then copy across the contents from the old one.

like image 571
thomasrutter Avatar asked Dec 20 '25 00:12

thomasrutter


1 Answers

Turns out that in PHP (and therefore, I guess, in libxml) you can add and remove DOMDocumentType objects to an existing document just like regular nodes using methods like $document->insertBefore() and $document->removeChild(), etc.

Seems to work OK - changing it changes the $document->doctype value and everything. It's quite undocumented though, and contradicts the W3 DOM standard which says it shouldn't be possible (in DOM Level 2 at least). But handy to know that you can do it in PHP.

One downside is that saveHTML() does not "see" the new doctype, even though saveXML() does.

like image 131
thomasrutter Avatar answered Dec 21 '25 16:12

thomasrutter