Preserve DOCTYPE of an lxml.etree parsed xml with namespace

488
December 15, 2016, at 12:44 PM

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?

READ ALSO
Cocotb using generics/parameters in gate level simulation

Cocotb using generics/parameters in gate level simulation

I have successfully setup a Cocotb verification environment for my design, and I am happy the way it works for RTL (VHDL in my case)

390
How to add whitespace after string.punctuation in Python?

How to add whitespace after string.punctuation in Python?

I want to clean my reviews dataHere's my code :

497
get the column from 2d array to calculate the normalization and cross product in python

get the column from 2d array to calculate the normalization and cross product in python

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on)

550