How to get the parent xml element after finding a child xml element using lxml and python
<p>I have a bit of a tough nut to crack.  It has been so long since I've used LXML that I need some help to get started.  I have an XML file that has a list of categories and proxy elements.  Please see the snippet below:</p>

<pre><code>&lt;categories&gt;
  &lt;category name="Light"&gt;
    &lt;proxy&gt;fan&lt;/proxy&gt;
  &lt;/category&gt;
  &lt;category name="UI"&gt;
    &lt;proxy&gt;doorbell&lt;/proxy&gt;
  &lt;/category&gt;
&lt;/categories&gt;
</code></pre>

<p>What I would like to do is search through all the proxy elements to find "doorbell".  If found, I would like to know the name of the parent element it came from.  So in the above example, doorbell would be found under the parent category elementnamed "UI".  In the end, I just need the value of the "name" attribute for the parent element in which the proxy fell under.</p>

<p>Any gurus out there want to help me tackle this?</p>

<p>If all you need is the name, might as well do it all in one search:</p>

<pre><code>import lxml.etree as ET

root = ET.XML('''
&lt;categories&gt;
  &lt;category name="Light"&gt;
    &lt;proxy&gt;fan&lt;/proxy&gt;
  &lt;/category&gt;
  &lt;category name="UI"&gt;
    &lt;proxy&gt;doorbell&lt;/proxy&gt;
  &lt;/category&gt;
&lt;/categories&gt;
''')

category_names = root.xpath(
  './/proxy[. = $proxy_type]/parent::category/@name',
  proxy_type='doorbell')

print category_names
</code></pre>

<p>...emits, as one would expect:</p>

<pre><code>['UI']
</code></pre>

python xml lxml