使用命名空间保留lxml.etree解析的xml的DOCTYPE

时间:2016-12-07 10:39:46

标签: python xml lxml doctype

此问题与Preserving original doctype and declaration of an lxml.etree parsed xml

几乎相同

我想保留带有命名空间的DOCTYPE声明,但上述问题的答案方法相同并不能保留声明。

$('.cat-center li').click(function() {
    event.stopPropagation();
    if ($(this).find('ul').is(":visible"))
    {
        $(this).find('ul').hide(500);
    }
    else
    {
      $('.cat-center li ul').hide(500);
      $(this).find('ul').toggle('blind');
    }
});

,输出为:

 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)

还有其他方法可以保留原始xml树的doctype声明吗?

0 个答案:

没有答案