我试图从XML文件中计算表单中的所有XML节点:
....
<node id="0">
<data key="d0">Attribute</data>
....
</node>
....
例如这样的文件:
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph edgedefault="directed">
<node id="0">
<data key="d0">Attribute</data>
<data key="d1">Foo</data>
</node>
我试过的是:
x = graphml_root.findall(".//"+nsfy("node")+"/["+nsfy("data")+"='Attribute']")
但他唯一的说法是XML的文本必须是&#34;属性&#34;,我想确保&#34;属性&#34;是key="d0"
节点的文本,所以我尝试了这个:
x = graphml_root.findall(".//"+nsfy("node")+"/"+nsfy("data")+"[@key='d0']"+"[""'Attribute']")
但它返回一个空列表,所以我错过了一些东西。
注意: 我不得不写一个小lambda来避免在整个时间内复制xmlnamespace:
nsfy = lambda x : '{http://graphml.graphdrawing.org/xmlns}'+x #to be able to read namespace tags
感谢。
答案 0 :(得分:1)
尝试做类似的事情:
nodes = []
containers = graphml_root.findall('.//node/data[@key="d0"]')
for container in containers:
if container.text == "Attribute":
nodes.append(container)
count = len(nodes)
答案 1 :(得分:1)
from lxml import etree
f= '''
<node id="0">
<data key="d0" t="32">Attribute</data>
<data key="d1">Foo</data>
</node>'''
root = etree.XML(f)
data = root.xpath('.//*[@key="d0" and text()="Attribute"]')
print(data)
lxml提供xpath
方法。它已完成。
更新
阅读xml.etree
的DOC,它不支持此语法。the xpath supported by xml.etree
所以,只有你能做的就是找.//*[@key="d0"]
然后测试它的文字等于&#34;属性&#34;。