2024-06-12 00:04:11 发布
网友
我试过了:
document.doctype = xml.dom.minidom.DocumentType('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"')
输出中没有doctype。如何在不手动插入的情况下进行修复?在
不应直接从minidom实例化类。它不是API支持的一部分,ownerDocument不会绑定,您可能会遇到一些奇怪的错误行为。相反,请使用正确的DOM级别2核心方法:
minidom
ownerDocument
>>> imp= minidom.getDOMImplementation('') >>> dt= imp.createDocumentType('html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
('DTD/xhtml1)-严格.dtd'是一个常用但错误的SystemId。该相对URL仅在w3.org的xhtml1文件夹内有效。)
SystemId
现在有了一个DocumentType节点,可以将其添加到文档中。根据标准,唯一有保证的方法是在文档创建时:
DocumentType
如果要更改现有文档的doctype,则会带来更大的麻烦。DOM标准不要求没有DocumentType的DocumentType节点可以插入到文档中。但是有些dom允许这样做,例如pxdom。minidom某种程度上允许它:
pxdom
>>> doc= minidom.parseString('<html xmlns="http://www.w3.org/1999/xhtml"><head/><body/></html>') >>> dt= minidom.getDOMImplementation('').createDocumentType('html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd') >>> doc.insertBefore(dt, doc.documentElement) <xml.dom.minidom.DocumentType instance> >>> print doc.toxml() <?xml version="1.0" ?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><html xmlns="http://www.w3.org/1999/xhtml"><head/><body/></html>
但是对于bug:
>>> doc.doctype # None >>> dt.ownerDocument # None
这对你来说可能重要也可能不重要。在
从技术上讲,根据标准在现有文档上设置doctype的唯一可靠方法是创建一个新文档并将整个旧文档导入其中!在
def setDoctype(document, doctype): imp= document.implementation newdocument= imp.createDocument(doctype.namespaceURI, doctype.name, doctype) newdocument.xmlVersion= document.xmlVersion refel= newdocument.documentElement for child in document.childNodes: if child.nodeType==child.ELEMENT_NODE: newdocument.replaceChild( newdocument.importNode(child, True), newdocument.documentElement ) refel= None elif child.nodeType!=child.DOCUMENT_TYPE_NODE: newdocument.insertBefore(newdocument.importNode(child, True), refel) return newdocument
不应直接从
minidom
实例化类。它不是API支持的一部分,ownerDocument
不会绑定,您可能会遇到一些奇怪的错误行为。相反,请使用正确的DOM级别2核心方法:('DTD/xhtml1)-严格.dtd'是一个常用但错误的
SystemId
。该相对URL仅在w3.org的xhtml1文件夹内有效。)现在有了一个
^{pr2}$DocumentType
节点,可以将其添加到文档中。根据标准,唯一有保证的方法是在文档创建时:如果要更改现有文档的doctype,则会带来更大的麻烦。DOM标准不要求没有
DocumentType
的DocumentType
节点可以插入到文档中。但是有些dom允许这样做,例如pxdom
。minidom
某种程度上允许它:但是对于bug:
这对你来说可能重要也可能不重要。在
从技术上讲,根据标准在现有文档上设置doctype的唯一可靠方法是创建一个新文档并将整个旧文档导入其中!在
相关问题 更多 >
编程相关推荐