How to tell lxml.etree.tostring(element) not to write namespaces in python?

后端 未结 3 376
你的背包
你的背包 2020-12-10 15:16

I have a huge xml file (1 Gig). I want to move some of the elements (entrys) to another file with the same header and specifications.

Let\'s say the original file co

3条回答
  •  醉话见心
    2020-12-10 16:12

    There is a way to remove namespaces with XSLT:

    import io
    import lxml.etree as ET
    
    
    def remove_namespaces(doc):
        # http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
        xslt='''
        
    
        
            
              
            
        
    
        
            
              
            
        
    
        
            
              
            
        
        
        '''
    
        xslt_doc = ET.parse(io.BytesIO(xslt))
        transform = ET.XSLT(xslt_doc)
        doc = transform(doc)
        return doc
    
    doc = ET.parse('data.xml')
    doc = remove_namespaces(doc)
    print(ET.tostring(doc))
    

    yields

    
    
    
        some text
    
    
    
    

提交回复
热议问题