3

This question is almost the same as Preserving original doctype and declaration of an lxml.etree parsed xml

I want to preserve DOCTYPE declaration with namespace, but the same way of answers of the question above does not preserve declaration.

 from lxml import etree
 from StringIO import StringIO

 xml_string = """<?xml version="1.0"?>                                                                
 <!DOCTYPE rdf:RDF [                                                                                  
     <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >                                               
     <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >                                     
 ]>                                                                                                   
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">                                    
   <a>&xsd;</a>                                                                                       
 </rdf:RDF>                                                                                           
 """

 parse_tree = etree.parse(StringIO(xml_string))
 encoding = parse_tree.docinfo.encoding
 result = etree.tostring(parse_tree, xml_declaration=True, encoding=encoding)
 print "%s\nparse ElementTree:\n%s\n" % ('-'*20, result)

and the output is:

 --------------------
 parse ElementTree:
 <?xml version='1.0' encoding='UTF-8'?>
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
   <a>http://www.w3.org/2001/XMLSchema#</a>                                                           
 </rdf:RDF>

Is there still any other way to preserve doctype declaration of original xml tree?

Community
  • 1
  • 1
furushchev
  • 2,539
  • 2
  • 10
  • 16

0 Answers0