有没有一种优雅的方法可以在 python 中使用 lxml 来计算 xml 文件中的标签元素?
问题描述
我可以将 xml 文件的内容读取为字符串并使用字符串操作来实现这一点,但我想有一种更优雅的方法可以做到这一点.由于我在文档中没有找到线索,所以我在这里:
I could read the content of the xml file to a string and use string operations to achieve this, but I guess there is a more elegant way to do this. Since I did not find a clue in the docus, I am sking here:
给定一个 xml(见下文)文件,您如何计算 xml 标签,例如 count of author-tags 在下面的示例中,最优雅的方式? 我们假设每个作者只出现一次.
Given an xml (see below) file, how do you count xml tags, like count of author-tags in the example bewlow the most elegant way? We assume, that each author appears exactly once.
<root>
<author>Tim</author>
<author>Eva</author>
<author>Martin</author>
etc.
</root>
这个xml文件很琐碎,但有可能,作者并不总是一个接一个地列出来,他们之间可能还有其他的标签.
This xml file is trivial, but it is possible, that the authors are not always listed one after another, there may be other tags between them.
解决方案
如果要统计所有作者标签:
If you want to count all author tags:
import lxml.etree
doc = lxml.etree.parse(xml)
count = doc.xpath('count(//author)')
相关文章