title: How to get xml elements which have childs with a certain tag and attribute
body: <p>I want to find xml elements which have certain child elements. The child elements need to have a given tag and an attribute set to a specific value.</p>
  <p>To give a concrete example (based on the <a href="https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml" rel="nofollow noreferrer">official documentation</a>). I want to find all <code>country</code> elements which have a child element <code>neighbor</code> with attribute <code>name=&quot;Austria&quot;</code>:</p>
  <pre><code>import xml.etree.ElementTree as ET
  
  data = &quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;data&gt;
      &lt;country name=&quot;Liechtenstein&quot;&gt;
          &lt;neighbor name=&quot;Austria&quot; direction=&quot;E&quot;/&gt;
          &lt;neighbor name=&quot;Switzerland&quot; direction=&quot;W&quot;/&gt;
      &lt;/country&gt;
      &lt;country name=&quot;Singapore&quot;&gt;
          &lt;neighbor name=&quot;Malaysia&quot; direction=&quot;N&quot;/&gt;
          &lt;partner name=&quot;Austria&quot;/&gt;
      &lt;/country&gt;
      &lt;country name=&quot;Panama&quot;&gt;
          &lt;neighbor name=&quot;Costa Rica&quot; direction=&quot;W&quot;/&gt;
          &lt;neighbor name=&quot;Colombia&quot; direction=&quot;E&quot;/&gt;
      &lt;/country&gt;
  &lt;/data&gt;
  &quot;&quot;&quot;
  
  root = ET.fromstring(data)
  </code></pre>
  <p>What I've tried without success:</p>
  <pre><code>countries1 = root.findall('.//country[neighbor@name=&quot;Austria&quot;]')
  countries2 = root.findall('.//country[neighbor][@name=&quot;Austria&quot;]')
  countries3 = root.findall('.//country[neighbor[@name=&quot;Austria&quot;]]')
  </code></pre>
  <p>which all give:</p>
  <blockquote>
  <p>SyntaxError: invalid predicate</p>
  </blockquote>
  <hr />
  <p>Following solutions are obviously wrong, as too much elements are found:</p>
  <pre><code>countries4 = root.findall('.//country/*[@name=&quot;Austria&quot;]')
  countries5 = root.findall('.//country/[neighbor]')
  </code></pre>
  <p>where <code>countries4</code> contains all elements having an attribute <code>name=&quot;Austria&quot;</code>, but including the <code>partner</code> element. <code>countries5</code> contains all elements which have <em>any</em> neighbor element as a child.</p>