2009-12-30 13 views
6

मैंने कोशिश की:पायथन के xml.dom.minidom के साथ एक डॉक्टरेट कैसे प्रस्तुत करें?

document.doctype = xml.dom.minidom.DocumentType('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"') 

उत्पादन में कोई doctype है। इसे हाथ से डालने के बिना कैसे ठीक करें?

उत्तर

11

आपको minidom से कक्षाओं को तुरंत चालू नहीं करना चाहिए। यह एपीआई का समर्थित हिस्सा नहीं है, ownerDocument एस टाई नहीं जाएगा और आप कुछ अजीब दुर्व्यवहार प्राप्त कर सकते हैं। एक सामान्य रूप से प्रयुक्त लेकिन गलत SystemId कि सापेक्ष URL केवल w3 पर xhtml1 फ़ोल्डर के अंदर मान्य होगा

>>> 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-strict.dtd' है:। इसके बजाय उचित डोम स्तर 2 कोर तरीकों का उपयोग। संगठन।)

अब आपके पास DocumentType नोड है, आप इसे किसी दस्तावेज़ में जोड़ सकते हैं। मानक के अनुसार, ऐसा करने का केवल गारंटी रास्ता दस्तावेज़ निर्माण समय पर है:

>>> doc= imp.createDocument('http://www.w3.org/1999/xhtml', 'html', dt) 
>>> 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/> 

यदि आप किसी मौजूदा दस्तावेज़ के doctype बदलना चाहते हैं, कि और अधिक परेशानी है। डीओएम मानक की आवश्यकता नहीं है DocumentType नोड्स ownerDocument किसी दस्तावेज़ में डालने योग्य नहीं है। हालांकि कुछ डीओएम इसे अनुमति देते हैं, उदाहरण के लिए। pxdomminidom तरह का यह अनुमति देता है:

>>> 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> 

लेकिन साथ कीड़े:

>>> doc.doctype 
# None 
>>> dt.ownerDocument 
# None 

जो या आपके लिए महत्वपूर्ण नहीं हो सकता है।

तकनीकी रूप से, मौजूदा दस्तावेज़ पर एक डॉक्टरेट सेट करने के लिए मानक के प्रति एकमात्र विश्वसनीय तरीका एक नया दस्तावेज़ बनाना और इसमें पुराने दस्तावेज़ को आयात करना है!

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 
+0

धन्यवाद! मुझे केवल नए दस्तावेज़ पर मौजूदा दस्तावेज़ पर डॉक्टरेट सेट करने की आवश्यकता नहीं है। – myfreeweb

+0

पुhew, वह भाग्यशाली है! :-) – bobince

संबंधित मुद्दे