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
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()