如何检查每个 <book> 元素在 xml 文件中是否有特定的子元素

How to check if each <book> element has a specific subchild in xml file

我想验证我的 XML 文件以检查每个 <book> 元素是否都有 <target> 的子元素,如果有任何缺失则抛出错误。

我的 XML 看起来像这样:

<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0"  version="2.0"><course id="cr1"><book id="bk1"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk2"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk3"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk4"><dek><source>ssf</source><target>ssf</target></dek></book>
</course>
<course id="cr2"><book id="bk1"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk2"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk3"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk4"><dek><source>ssf</source><target>ssf</target></dek></book>
</course>
</xliff>

有人可以建议我如何使用 etree.ElementTree

进行此操作吗

我试过了,是否可以一次完成?

   count_books = len(tree.findall(".//books"))
   count_target = len(tree.findall(".//target"))
   if (count_books != count_target):

ElementTree's XPath support 非常有限,所以我认为您无法通过单个 findall 调用来完成。

如果可以切换到 lxml,则可以使用 xpath() 并在一次调用中完成...

from lxml import etree

xml = """<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0"  version="2.0"><course id="cr1"><book id="bk1"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk2"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk3"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk4"><dek><source>ssf</source><target>ssf</target></dek></book>
</course>
<course id="cr2"><book id="bk1"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk2"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk3"><dek><source>ssf</source><target>ssf</target></dek></book>
<book id="bk4"><dek><source>ssf</source><target>ssf</target></dek></book>
</course>
</xliff>
"""

tree = etree.fromstring(xml)

ns = {"x": "urn:oasis:names:tc:xliff:document:2.0"}

bad_books = tree.xpath('.//x:book[not(.//x:target)]', namespaces=ns)

print(f"Are there any book elements without a target? - {bool(bad_books)}")

这将 return:

Are there any book elements without a target? - False

与当前输入。如果您删除 target(或重命名),它将 return:

Are there any book elements without a target? - True