【发布时间】:2015-04-08 10:22:17
【问题描述】:
我整个上午都在为此苦苦挣扎,但我无法让它发挥作用。
我有一个这样的 XML(精简匿名版本):
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<First_Level_Node>
<Element_Name>
<attribute1>1</attribute1>
<attribute2>2</attribute2>
<attribute3>3</attribute3>
<attribute4>4</attribute4>
<attribute5>5</attribute5>
<attribute6>6</attribute6>
</Element_Name>
<Element_Name>
<attribute1>42</attribute1>
<attribute2></attribute2>
<attribute3>NO</attribute3>
<attribute4>42</attribute4>
<attribute5>random value</attribute5>
<attribute6>18th Jun 2014 07:09:18 GMT</attribute6>
</Element_Name>
<Element_Name>
<attribute1>42</attribute1>
<attribute2></attribute2>
<attribute3>NO</attribute3>
<attribute4>42</attribute4>
<attribute5>random</attribute5>
<attribute6>23rd Jul 2014 02:47:10 GMT</attribute6>
</Element_Name>
<Element_Name>
<attribute1>42</attribute1>
<attribute2></attribute2>
<attribute3>NO</attribute3>
<attribute4>42</attribute4>
<attribute5>random</attribute5>
<attribute6>08th Nov 2014 23:53:31 GMT</attribute6>
</Element_Name>
</First_Level_Node>
</Root>
现在我已经从所有元素中获取了一些值并使用它们。
但现在我只想选择具有特定属性值对的元素。
例如,在我粘贴的 xml 中,我只需要使用 attribute4 = 42
获取元素我目前的代码如下:
tree=ET.parse('xmlname.xml')
root=tree.getroot()
for slot in input_data:
for child in root[0]:
for ch in child.findall('First Level Node/*/[@attribute4="' + str(sys.argv[1]) + '"]'):
print ch
if ch.tag == slot:
if ch.text == 'UNCOMPUTED' or ch.text == None:
slot_text.append("Undefined")
else:
slot_text.append(ch.text)
data[slot]=Counter(slot_text).most_common()
但我在 ch 中没有得到任何值。我已经尝试了相同的多种变体以及我所知道的所有 Xpath,但仍然没有结果。
任何帮助将不胜感激。
注意:Element_Name 是动态的,可以更改。
编辑:试过了,但输出的信息有误。
for slot in input_data:
for child in root[0]:
for ch in child:
if ch.text == '42' and ch.tag == "attribute4":
flag=1
if ch.tag == slot and flag == 1:
flag=0
if ch.text == 'UNCOMPUTED' or ch.text == None:
slot_text.append("Undefined")
else:
slot_text.append(ch.text)
data[slot]=Counter(slot_text).most_common()
【问题讨论】:
标签: python xml xpath elementtree