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.
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.
It also seems possible to call $document->loadXML() again even if the document is already populated. It wipes the document and regenerates it, but it is a way of changing the doctype variable on an existing object, and the new doctype does have an HTML representation unlike in my other solution.
精彩评论