def removeNestedGroups(node):
 global numElemsRemoved
 num = 0
 groupsToRemove = []
 if (not ((node.nodeType == 1) and (node.nodeName == 'switch'))):
  for child in node.childNodes:
   if ((child.nodeName == 'g') and (child.namespaceURI == NS['SVG']) and (len(child.attributes) == 0)):
    for grandchild in child.childNodes:
     if ((grandchild.nodeType == 1) and (grandchild.namespaceURI == NS['SVG']) and (grandchild.nodeName in ['title', 'desc'])):
      break
    else:
     groupsToRemove.append(child)
 for g in groupsToRemove:
  while (g.childNodes.length > 0):
   g.parentNode.insertBefore(g.firstChild, g)
  g.parentNode.removeChild(g)
  numElemsRemoved += 1
  num += 1
 for child in node.childNodes:
  if (child.nodeType == 1):
   num += removeNestedGroups(child)
 return num

def removeNestedGroups ( node ) : global numElemsRemoved num = 0 groupsToRemove = [ ] if ( not ( ( node . nodeType == 1 ) and ( node . nodeName == 'switch' ) ) ) : for child in node . childNodes : if ( ( child . nodeName == 'g' ) and ( child . namespaceURI == NS [ 'SVG' ] ) and ( len ( child . attributes ) == 0 ) ) : for grandchild in child . childNodes : if ( ( grandchild . nodeType == 1 ) and ( grandchild . namespaceURI == NS [ 'SVG' ] ) and ( grandchild . nodeName in [ 'title' , 'desc' ] ) ) : break else : groupsToRemove . append ( child ) for g in groupsToRemove : while ( g . childNodes . length > 0 ) : g . parentNode . insertBefore ( g . firstChild , g ) g . parentNode . removeChild ( g ) numElemsRemoved += 1 num += 1 for child in node . childNodes : if ( child . nodeType == 1 ) : num += removeNestedGroups ( child ) return num
this walks further and further down the tree .