def removeUnusedAttributesOnParent(elem):
    """
    This recursively calls this function on all children of the element passed in,
    then removes any unused attributes on this elem if none of the children inherit it
    """
    num = 0

    childElements = []
    # recurse first into the children (depth-first)
    for child in elem.childNodes:
        if child.nodeType == Node.ELEMENT_NODE:
            childElements.append(child)
            num += removeUnusedAttributesOnParent(child)

    # only process the children if there are more than one element
    if len(childElements) <= 1:
        return num

    # get all attribute values on this parent
    attrList = elem.attributes
    unusedAttrs = {}
    for index in range(attrList.length):
        attr = attrList.item(index)
        if attr.nodeName in ['clip-rule',
                             'display-align',
                             'fill', 'fill-opacity', 'fill-rule',
                             'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch',
                             'font-style', 'font-variant', 'font-weight',
                             'letter-spacing',
                             'pointer-events', 'shape-rendering',
                             'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin',
                             'stroke-miterlimit', 'stroke-opacity', 'stroke-width',
                             'text-anchor', 'text-decoration', 'text-rendering', 'visibility',
                             'word-spacing', 'writing-mode']:
            unusedAttrs[attr.nodeName] = attr.nodeValue

    # for each child, if at least one child inherits the parent's attribute, then remove
    for childNum in range(len(childElements)):
        child = childElements[childNum]
        inheritedAttrs = []
        for name in unusedAttrs:
            val = child.getAttribute(name)
            if val == '' or val is None or val == 'inherit':
                inheritedAttrs.append(name)
        for a in inheritedAttrs:
            del unusedAttrs[a]

    # unusedAttrs now has all the parent attributes that are unused
    for name in unusedAttrs:
        elem.removeAttribute(name)
        num += 1

    return num
def removeUnusedAttributesOnParent ( elem ) : num = 0 childElements = [ ] for child in elem . childNodes : if child . nodeType == Node . ELEMENT_NODE : childElements . append ( child ) num += removeUnusedAttributesOnParent ( child ) if len ( childElements ) <= 1 : return num attrList = elem . attributes unusedAttrs = { } for index in range ( attrList . length ) : attr = attrList . item ( index ) if attr . nodeName in [ 'clip-rule' , 'display-align' , 'fill' , 'fill-opacity' , 'fill-rule' , 'font' , 'font-family' , 'font-size' , 'font-size-adjust' , 'font-stretch' , 'font-style' , 'font-variant' , 'font-weight' , 'letter-spacing' , 'pointer-events' , 'shape-rendering' , 'stroke' , 'stroke-dasharray' , 'stroke-dashoffset' , 'stroke-linecap' , 'stroke-linejoin' , 'stroke-miterlimit' , 'stroke-opacity' , 'stroke-width' , 'text-anchor' , 'text-decoration' , 'text-rendering' , 'visibility' , 'word-spacing' , 'writing-mode' ] : unusedAttrs [ attr . nodeName ] = attr . nodeValue for childNum in range ( len ( childElements ) ) : child = childElements [ childNum ] inheritedAttrs = [ ] for name in unusedAttrs : val = child . getAttribute ( name ) if val == '' or val is None or val == 'inherit' : inheritedAttrs . append ( name ) for a in inheritedAttrs : del unusedAttrs [ a ] for name in unusedAttrs : elem . removeAttribute ( name ) num += 1 return num
This recursively calls this function on all children of the element passed in,
    then removes any unused attributes on this elem if none of the children inherit it