给定 python 中的值,如何获取标签名称?

How to get the tag name given the value in python?

我的 xml 文件看起来与此相似

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder|Remind|Remain</heading>
<body>Don't forget me this weekend!</body>
</note>

我想要给定字符串时的标签名称。例如,如果我指定像“|”这样的字符串,我想要标签,即 heading。 如何在 python 中实现此目的?

更简单的尝试可能是-

import lxml.etree as et

s="""
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder|Remind|Remain</heading>
<body>Don't forget me this weekend!</body>
</note>
"""

tree = et.fromstring(s)

print tree.text
query = r'%s'%raw_input("Enter text: ")

pth = r'''//*[contains(text(),'%s')]'''%query

for t in tree.xpath(pth):
    print t.tag

如果将 | 作为输入给出,则它会打印 heading