python - how to write empty tree node as empty string to xml file

后端 未结 3 1232
花落未央
花落未央 2021-01-27 01:25

I want to remove elements of a certain tag value and then write out the .xml file WITHOUT any tags for those deleted elements; is my only option to create a new tre

3条回答
  •  猫巷女王i
    2021-01-27 01:48

    Whenever modifying XML documents is needed, consider also XSLT, the special-purpose language part of the XSL family which includes XPath. XSLT is designed specifically to transform XML files. Pythoners are not quick to recommend it but it avoids the need of loops or nested if/then logic in general purpose code. Python's lxml module can run XSLT 1.0 scripts using the libxslt processor.

    Below transformation runs the identity transform to copy document as is and then runs an empty template match on to remove it:

    XSLT Script (save as an .xsl file to be loaded just like source .xml, both of which are well-formed xml files)

    
    
    
    
      
      
        
          
        
      
    
        
      
    
    
    

    Python Script

    import lxml.etree as et
    
    # LOAD XML AND XSL DOCUMENTS
    xml  = et.parse("Input.xml")
    xslt = et.parse("Script.xsl")
    
    # TRANSFORM TO NEW TREE
    transform = et.XSLT(xslt)
    newdom = transform(xml)
    
    # CONVERT TO STRING
    tree_out = et.tostring(newdom, encoding='UTF-8', pretty_print=True,  xml_declaration=True)
    
    # OUTPUT TO FILE
    xmlfile = open('Output.xml'),'wb')
    xmlfile.write(tree_out)
    xmlfile.close()
    

提交回复
热议问题