title: Select a node with a particular element value in XML using python 2.7 and XML element tree
body: <p>I've been struggling with this all morning and I haven't been able to get it to work.</p>
  
  <p>I have an XML like this(stripped down anonymized version):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Root&gt;
    &lt;First_Level_Node&gt;
      &lt;Element_Name&gt;
        &lt;attribute1&gt;1&lt;/attribute1&gt;
        &lt;attribute2&gt;2&lt;/attribute2&gt;
        &lt;attribute3&gt;3&lt;/attribute3&gt;
        &lt;attribute4&gt;4&lt;/attribute4&gt;
        &lt;attribute5&gt;5&lt;/attribute5&gt;
        &lt;attribute6&gt;6&lt;/attribute6&gt;
      &lt;/Element_Name&gt;
      &lt;Element_Name&gt;
        &lt;attribute1&gt;42&lt;/attribute1&gt;
        &lt;attribute2&gt;&lt;/attribute2&gt;
        &lt;attribute3&gt;NO&lt;/attribute3&gt;
        &lt;attribute4&gt;42&lt;/attribute4&gt;
        &lt;attribute5&gt;random value&lt;/attribute5&gt;
        &lt;attribute6&gt;18th Jun 2014  07:09:18 GMT&lt;/attribute6&gt;
      &lt;/Element_Name&gt;
      &lt;Element_Name&gt;
        &lt;attribute1&gt;42&lt;/attribute1&gt;
        &lt;attribute2&gt;&lt;/attribute2&gt;
        &lt;attribute3&gt;NO&lt;/attribute3&gt;
        &lt;attribute4&gt;42&lt;/attribute4&gt;
        &lt;attribute5&gt;random&lt;/attribute5&gt;
        &lt;attribute6&gt;23rd Jul 2014  02:47:10 GMT&lt;/attribute6&gt;
      &lt;/Element_Name&gt;
      &lt;Element_Name&gt;
        &lt;attribute1&gt;42&lt;/attribute1&gt;
        &lt;attribute2&gt;&lt;/attribute2&gt;
        &lt;attribute3&gt;NO&lt;/attribute3&gt;
        &lt;attribute4&gt;42&lt;/attribute4&gt;
        &lt;attribute5&gt;random&lt;/attribute5&gt;
        &lt;attribute6&gt;08th Nov 2014  23:53:31 GMT&lt;/attribute6&gt;
      &lt;/Element_Name&gt;
    &lt;/First_Level_Node&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Now I am already getting some values from all the elements and using them.</p>
  
  <p>But now I want to select only the elements which have a certain attribute value pair.</p>
  
  <p>For example in the xml I have pasted I need to get the elements only with <strong>attribute4 = 42</strong></p>
  
  <p>My current code is as follows:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help will be much appreciated.</p>
  
  <p>NOTE: Element_Name is dynamic and can change.</p>
  
  <p>EDIT: Tried this but am getting wrong info as output.</p>
  
  <pre><code>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()
  </code></pre>