title: Selecting parent element when child has a specific value of attribute using elementree xpath in Python
body: <p>I'm trying to use XPath expressions using Python's ElementTree. I'm having difficulty extracting parent elements whos chilrens has a specific attribute value. These are not in the examples <a href="https://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow noreferrer">here</a>, and I also tried <a href="https://stackoverflow.com/questions/1457638/xpath-get-nodes-where-child-node-contains-an-attribute">this</a>, this doesn't work.</p>
  
  <p>My XML doc looks as follows:</p>
  
  <pre><code>&lt;Transactions&gt;
  &lt;Transaction&gt;
      &lt;Project code="abc"&gt;
          &lt;Description&gt;blah blah&lt;/Description&gt;
          &lt;StartDate&gt;2014-10-02&lt;/StartDate&gt;
      &lt;/Project&gt;
      &lt;Quantity&gt;100&lt;/Quantity&gt;
      &lt;Price&gt;
          &lt;Currency code="EUR" /&gt;
          &lt;Value&gt;100&lt;/Value&gt;
      &lt;/Price&gt;
  &lt;/Transaction&gt;
  &lt;Transaction&gt;
      &lt;Project code="def"&gt;
          &lt;Description&gt;something else&lt;/Description&gt;
          &lt;StartDate&gt;2014-10-12&lt;/StartDate&gt;
      &lt;/Project&gt;
      &lt;Quantity&gt;4&lt;/Quantity&gt;
      &lt;Price&gt;
          &lt;Currency code="EUR" /&gt;
          &lt;Value&gt;2&lt;/Value&gt;
      &lt;/Price&gt;
  &lt;/Transaction&gt;
  &lt;Transaction&gt;
      &lt;Project code="abc"&gt;
          &lt;Description&gt;blah blah&lt;/Description&gt;
          &lt;StartDate&gt;2014-11-02&lt;/StartDate&gt;
      &lt;/Project&gt;
      &lt;Quantity&gt;1&lt;/Quantity&gt;
      &lt;Price&gt;
          &lt;Currency code="EUR" /&gt;
          &lt;Value&gt;123&lt;/Value&gt;
      &lt;/Price&gt;
  &lt;/Transaction&gt;&lt;/Transactions&gt;
  </code></pre>
  
  <p>I'm trying to select all Transaction elements whos Project has an code of "abc".</p>
  
  <p>I defined my root as follows:</p>
  
  <pre><code>transactions = ET.parse('../doc.xml').getroot();
  </code></pre>
  
  <p>This works (which returns all Transaction elements which have a Project as a child):</p>
  
  <pre><code>transactions.findall("Transaction[Project]")
  </code></pre>
  
  <p>This also works (which returns all Project elements which have the code of "abc"):</p>
  
  <pre><code>transactions.findall(".//Project[@code='abc']")
  </code></pre>
  
  <p>However I'm kind of lost on how to combine these (to get the Transaction elements whos child Project has a specific code). This doesn't work:</p>
  
  <pre><code>transactions.findall("Transaction[Project[@code='abc']]")
  </code></pre>
  
  <p>Nor this (which is discussed <a href="https://stackoverflow.com/questions/1457638/xpath-get-nodes-where-child-node-contains-an-attribute">here</a>):</p>
  
  <pre><code>transactions.findall("Transaction[Project/@code='abc']")
  </code></pre>
  
  <p>I have already spend more than 4 hours trying to solve this problem :(. If anyone is able to answer this question for me that would be very helpful!</p>
  
  <p>Kind regards,</p>
  
  <p>Diederik</p>