有些 element.tail 属性为空,但它们不应该

Some element.tail attributes are empty although they shouldn't

我正在尝试使用 python 3.4 中的 xml.etree.ElementTree 来解析 large XML file(使用一本圣经)(为了与 Windows 兼容,我宁愿留在标准库模块),相关方法在这里

class BibleTree:
    def __init__(self, file_name: str) -> None:
        self.root = ET.parse(file_name).getroot()

    @staticmethod
    def _list_to_clean_text(str_in: str) -> str:
        out = re.sub(r'[\s\n]+', ' ', str_in, flags=re.DOTALL)
        return out.strip()

    @staticmethod
    def _clean_text(intext: Optional[str]) -> str:
        return intext if intext is not None else ''

    def __iter__(self) -> Tuple[int, int, str]:
        collected = None
        cur_chap = 0
        cur_verse = 0

        for child in self.root:
            if child.tag in ['kap', 'vers']:
                if collected and collected.strip():
                    yield cur_chap, cur_verse, self._list_to_clean_text(collected)
                if child.tag == 'kap':
                    cur_chap = int(child.attrib['n'])
                elif child.tag == 'vers':
                    cur_verse = int(child.attrib['n'])
                collected = self._clean_text(child.tail)
            else:
                if collected is not None:
                    collected += self._clean_text(child.text)
                    collected += self._clean_text(child.tail)

问题是在某些情况下(例如,第 54 行的元素 <odkazo/>)变量 childtail 属性是 None,尽管它应该是恕我直言的文本。

有什么想法,我做错了什么吗?

这是 PEBKAC ...我假设其他元素中没有里程碑元素。所以,我需要将整个函数重写为递归函数。好吧。