Given a parent element, replace oldelem with newelem.

def replaceelement(oldelem, newelem):
    '''
    Given a parent element, replace oldelem with newelem.
    '''
    parent = oldelem.getparent()
    if parent is not None:
        size = len(parent.getchildren())
        for x in range(0, size):
            if parent.getchildren()[x] == oldelem:
                parent.remove(oldelem)
                parent.insert(x, newelem)