etree.iterwalk doesn't walk through the processing instructions since lxml v4.4

446 Views Asked by At

Before lxml v4.4, lxml.etree.iterwalk walks through elements and processing instructions:

With the following code:

from lxml import etree

XML = """<root>
  <?page no="1"?>
  <element key='value'>text</element>
  <element>text</element>tail<?page no="2"?>
  <empty-element xmlns="http://testns/"/>
</root>"""

context = etree.iterwalk(etree.XML(XML), events=("start",))
for action, elem in context:
    if isinstance(elem, etree._ProcessingInstruction):
        print(f"{action}: {elem.target} {elem.text}")
    else:
        print(f"{action}: {elem.tag}")

I had:

start: root
start: page no="1"
start: element
start: element
start: page no="2"
start: {http://testns/}empty-element

But since lxml v4.4 and above, I have:

start: root
start: element
start: element
start: {http://testns/}empty-element

Processing instructions are now ignored. Why?

How can I restore the previous behavior?

0

There are 0 best solutions below