def recursively_prune_elements(soup):
    """Recursively prune out any elements which have no children or only zero-length children."""
    def single_replace():
        n_removed = 0
        # Remove elements with no children
        for element in soup.find_all(lambda elem: len(list(elem.children)) == 0):
            element.decompose()
            n_removed += 1
        # Remove elements with only zero-length children
        for element in soup.find_all(lambda elem: sum([len(c) for c in elem.children]) == 0):
            element.decompose()
            n_removed += 1
        return n_removed
    # Repeatedly apply single_replace() until no elements are being removed
    while single_replace():
        pass