Skip to content

Instantly share code, notes, and snippets.

@ruffsl
Last active July 28, 2018 01:03
Show Gist options
  • Save ruffsl/251d10ec2a815e0d6c9c5a8002ba3036 to your computer and use it in GitHub Desktop.
Save ruffsl/251d10ec2a815e0d6c9c5a8002ba3036 to your computer and use it in GitHub Desktop.
Using XML schema to find and enable substitution of missing elements
import xml.etree.ElementTree as ElementTree
from xml.dom import minidom
def tidy_xml(element):
subiter = ElementTree.ElementTree(element).getiterator()
for x in subiter:
if len(x):
if x.text:
x.text = x.text.strip()
if x.tail:
x.tail = x.tail.strip()
return element
def pretty_xml(element):
xmlstr = ElementTree.tostring(element, encoding='unicode', method='xml')
xmlstr = minidom.parseString(xmlstr).toprettyxml(indent=' ', newl='\n', encoding='utf-8')
return xmlstr.decode('utf-8')
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment