Python 使用lxml库解析xliff文件

Python 使用lxml库解析xliff文件,python,xml,parsing,lxml,xliff,Python,Xml,Parsing,Lxml,Xliff,我无法解析这个xliff片段: <source>text1 <g id="1">text2</g> text3 <x id="2"/><x id="3"/>text4</source> 我得到类似于: end: source text1 end: g text2 end: x None end: x None 我无法解析text3和text4…我该怎么做?谢谢您需要考虑tail属性(元素后面的文本)。请在此处阅读: 下面

我无法解析这个xliff片段:

<source>text1 <g id="1">text2</g> text3 <x id="2"/><x id="3"/>text4</source>
我得到类似于:

end: source text1
end: g text2
end: x None
end: x None

我无法解析
text3
text4
…我该怎么做?谢谢

您需要考虑
tail
属性(元素后面的文本)。请在此处阅读:

下面的代码片段(对代码的轻微修改)演示了这一点:

from lxml import etree

tree = etree.iterparse('aFile.xlf')
for action, elem in tree:
    print("%s: %s %s %s" % (action, elem.tag, elem.text, elem.tail))
输出:

end: g text2  text3 
end: x None None
end: x None text4
end: source text1  None
from lxml import etree

tree = etree.iterparse('aFile.xlf')
for action, elem in tree:
    print("%s: %s %s %s" % (action, elem.tag, elem.text, elem.tail))
end: g text2  text3 
end: x None None
end: x None text4
end: source text1  None