def parse_and_get_ns(xml): events = "start", "start-ns" root = None ns = {} for event, elem in et.iterparse(xml, events): if event == "start-ns": if elem[0] in ns and ns[elem[0]] != elem[1]: raise KeyError("Duplicate prefix with different URI found.") ns[elem[0]] = "%s" % elem[1] elif event == "start": if root is None: root = elem return et.ElementTree(root), ns
def parse_and_get_ns(file): events = "start", "start-ns" root = None ns = {} for event, elem in et.iterparse(file, events): if event == "start-ns": if elem[0] in ns and ns[elem[0]] != elem[1]: # NOTE: It is perfectly valid to have the same prefix refer # to different URI namespaces in different parts of the # document. This exception serves as a reminder that this # solution is not robust. Use at your own peril. raise KeyError("Duplicate prefix with different URI found.") ns[elem[0]] = "%s" % elem[1] elif event == "start": if root is None: root = elem return et.ElementTree(root), ns