code: def trim_tree_before(element, include_element=True, keep_head=True):
      
      el = element
      for parent_el in element.iterancestors():
          parent_el.text = None
          if el != element or include_element:
              el = el.getprevious()
          else:
              parent_el.text = el.tail
          while el is not None:
              remove_el = el
              el = el.getprevious()
              tag = remove_el.tag
              is_head = isinstance(tag, string_class) and tag.lower() == 'head'
              if not keep_head or not is_head:
                  parent_el.remove(remove_el)
          el = parent_el
docs: Removes the document tree preceding the given element. If include_element
      is True, the given element is kept in the tree, otherwise it is removed.