I've been struggling with this all morning and I haven't been able to get it to work.
I have an XML like this(stripped down anonymized version):
<?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>
Now I am already getting some values from all the elements and using them.
But now I want to select only the elements which have a certain attribute value pair.
For example in the xml I have pasted I need to get the elements only with attribute4 = 42
My current code is as follows:
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()
But I don't get any values in ch. I have tried multiple variations of the same and all the Xpath I know, still no result.
Any help will be much appreciated.
NOTE: Element_Name is dynamic and can change.
EDIT: Tried this but am getting wrong info as output.
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()