from xml.etree.ElementTree import fromstring, ElementTree, Element

def validate_node(elem):
    for child in elem.getchildren():
        validate_node(child)
value = child.attrib.get('value', '')
if not value.isdigit() or int(value) < 5:
    child.append(Element('D', {'name': 'error'}))
if __name__ == '__main__':
    import sys
xml = sys.stdin.read()
root = fromstring(xml)
validate_node(root)
ElementTree(root).write(sys.stdout, encoding='utf-8')
Вставьте узел для элемента в XML с помощью Python/ElementTree