代码之家  ›  专栏  ›  技术社区  ›  thomasrutter

php dom:更改现有domdocument的doctype

  •  2
  • thomasrutter  · 技术社区  · 14 年前

    使用创建DOMDocument时 DOMImplementation::createDocument() ,可以将doctype指定为构造函数中的第三个参数。然后,此doctype将“绑定”到文档,您以后可以使用 $document->doctype .

    但是,这是一个只读属性(与编码和版本不同!).

    是否有任何方法可以更改现有domdocument对象的doctype属性?

    我唯一能想到的解决方法是用新的doctype创建一个新的domdocument,然后导入并复制旧文档中的内容。

    2 回复  |  直到 14 年前
        1
  •  1
  •   thomasrutter    14 年前

    结果表明,在PHP(因此,我猜,在libxml)中,您可以使用如下方法向现有文档添加和删除domdocumenttype对象,就像常规节点一样 $document->insertBefore() $document->removeChild() 等。

    似乎工作正常-更改它会更改$document->doctype值和所有内容。但它是完全没有文档的,并且与W3DOM标准相矛盾,后者认为它不可能实现(至少在DOM级别2中)。但是很容易知道你可以用PHP来实现。

    一个缺点是savehtml()不能“看到”新的doctype,即使savexml()看到了。

        2
  •  1
  •   thomasrutter    14 年前

    即使文档已经填充,也可能再次调用$document->loadXML()。它将擦除文档并重新生成文档,但这是更改现有对象上doctype变量的一种方法,而且新doctype确实具有HTML表示形式,这与我的其他解决方案不同。