1

lxml v4.4 之前,lxml.etree.iterwalk遍历元素处理指令:

使用以下代码:

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}")

我有:

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

但自从 lxml v4.4 及更高版本以来,我有:

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

现在忽略处理指令。为什么?

如何恢复以前的行为?

4

0 回答 0