With lxml's API it's possible to access preliminary elements before the root such as processing instructions and comments by querying the sibling (and its siblings in turn) of the root element:
>>> buffer = StringIO('<?xml version="1.0" ?><?PI foo?><root/>')
>>> doc = etree.parse(buffer)
>>> root = doc.getroot()
>>> pi = root.getprevious()
>>> str(pi)
'<?PI foo?>'
In order to remove an element from a document however there must be a parent element involved:
parent.remove(child)
Apparently there's no parent available for that processing instruction element. I could deepcopy the root to get another tree without the previous siblings, but this is obviously not a preferable solution. Does anyone know a solution to achieve such removal that doesn't require an explaining comment in the code? (Shifting the explanation to a function name wouldn't count as an answer.)