def annotate_with_XMLNS(tree, prefix, URI):
"""
Annotates the provided DOM tree with XMLNS attributes and adds XMLNS
prefixes to the tags of the tree nodes.
:param tree: the input DOM tree
:type tree: an ``xml.etree.ElementTree.ElementTree`` or
``xml.etree.ElementTree.Element`` object
:param prefix: XMLNS prefix for tree nodes' tags
:type prefix: str
:param URI: the URI for the XMLNS definition file
:type URI: str
"""
if not ET.iselement(tree):
tree = tree.getroot()
tree.attrib['xmlns:' + prefix] = URI
iterator = tree.iter()
next(iterator) # Don't add XMLNS prefix to the root node
for e in iterator:
e.tag = prefix + ":" + e.tag
评论列表
文章目录