def stripWhitespace(element, tab=0):
    element.normalize()

    lastSpacer = "\n" + ("\t"*tab)
    spacer = lastSpacer + "\t"

    #Zero children aren't allowed (i.e. <empty/>)
    #This makes writing output simpler, and matches Canonical XML
    if element.childNodes.length==0: #DON'T DO len(element.childNodes) - doesn't work in Jython
        raise SyntaxError("Empty XML elements not allowed")

    #If there's a single child, it must be text context
    if element.childNodes.length==1:
        if element.firstChild.nodeType == element.firstChild.TEXT_NODE:
            #If it's an empty element, remove
            if element.firstChild.data == lastSpacer:
                element.removeChild(element.firstChild)
            return
        #If not text content, give an error
        elif element.firstChild.nodeType == element.firstChild.ELEMENT_NODE:
            raise SyntaxError("Bad whitespace under '%s'" % element.tagName)
        else:
            raise SyntaxError("Unexpected node type in XML document")

    #Otherwise there's multiple child element
    child = element.firstChild
    while child:
        if child.nodeType == child.ELEMENT_NODE:
            stripWhitespace(child, tab+1)
            child = child.nextSibling
        elif child.nodeType == child.TEXT_NODE:
            if child == element.lastChild:
                if child.data != lastSpacer:
                    raise SyntaxError("Bad whitespace under '%s'" % element.tagName)
            elif child.data != spacer:
                raise SyntaxError("Bad whitespace under '%s'" % element.tagName)
            next = child.nextSibling
            element.removeChild(child)
            child = next
        else:
            raise SyntaxError("Unexpected node type in XML document")
dataset/ETHPy150Open acil-bwh/SlicerCIP/Scripted/attic/PicasaSnap/gdata/tlslite/utils/xmltools.py/stripWhitespace/original