def removeUnreferencedElements(doc):
 global numElemsRemoved
 num = 0
 removeTags = ['linearGradient', 'radialGradient', 'pattern']
 identifiedElements = findElementsWithId(doc.documentElement)
 referencedIDs = findReferencedElements(doc.documentElement)
 for id in identifiedElements:
  if (not (id in referencedIDs)):
   goner = identifiedElements[id]
   if ((goner != None) and (goner.parentNode != None) and (goner.nodeName in removeTags)):
    goner.parentNode.removeChild(goner)
    num += 1
    numElemsRemoved += 1
 defs = doc.documentElement.getElementsByTagName('defs')
 for aDef in defs:
  elemsToRemove = removeUnusedDefs(doc, aDef)
  for elem in elemsToRemove:
   elem.parentNode.removeChild(elem)
   numElemsRemoved += 1
   num += 1
 return num

def removeUnreferencedElements ( doc ) : global numElemsRemoved num = 0 removeTags = [ 'linearGradient' , 'radialGradient' , 'pattern' ] identifiedElements = findElementsWithId ( doc . documentElement ) referencedIDs = findReferencedElements ( doc . documentElement ) for id in identifiedElements : if ( not ( id in referencedIDs ) ) : goner = identifiedElements [ id ] if ( ( goner != None ) and ( goner . parentNode != None ) and ( goner . nodeName in removeTags ) ) : goner . parentNode . removeChild ( goner ) num += 1 numElemsRemoved += 1 defs = doc . documentElement . getElementsByTagName ( 'defs' ) for aDef in defs : elemsToRemove = removeUnusedDefs ( doc , aDef ) for elem in elemsToRemove : elem . parentNode . removeChild ( elem ) numElemsRemoved += 1 num += 1 return num
removes all unreferenced elements except for <svg> .