python lxml get parent element when you know child text with xpath
Question: I have the following xml file: _test.xml_

    
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <SubmitTransaction xmlns="http://www.someaddress.com/someendpoint">
          <objTransaction>
            <DataFields>
              <TxnField>
                <FieldName>Pickup.Address.CountryCode</FieldName>
                <FieldValue>DE</FieldValue>
                <FieldIndex>0</FieldIndex>
              </TxnField>
              <TxnField>
                <FieldName>Pickup.Address.PostalCode</FieldName>
                <FieldValue>10827</FieldValue>
                <FieldIndex>0</FieldIndex>
              </TxnField>
              <TxnField>
                <FieldName>Pickup.DateTime</FieldName>
                <FieldValue>2016-05-28T03:26:05</FieldValue>
                <FieldIndex>0</FieldIndex>
              </TxnField>
              <TxnField>
                <FieldName>Pickup.LocationTypeCode</FieldName>
                <FieldValue>O</FieldValue>
                <FieldIndex>0</FieldIndex>
              </TxnField>
              <TxnField>
                <FieldName>Pickup.Address.City</FieldName>
                <FieldValue>Berlin</FieldValue>
                <FieldIndex>0</FieldIndex>
              </TxnField>
            </DataFields>
          </objTransaction>
        </SubmitTransaction>
      </soap:Body>
    </soap:Envelope>
    

What I want to do is to get an element with tag `TxnField` that has a child
`FieldName` with text `Pickup.DateTime`. It is important to get the parent
element, so I need to get this:

    
    
    <TxnField>
      <FieldName>Pickup.DateTime</FieldName>
      <FieldValue>2016-05-28T03:26:05</FieldValue>
      <FieldIndex>0</FieldIndex>
    </TxnField>
    

What I have so far is the following:

    
    
    from lxml import etree
    xml_parser = etree.XMLParser(remove_blank_text=True)
    xml_tree = etree.parse('test.xml', xml_parser)
    
    p_time = xml_tree.xpath("//*[local-name()='TxnField']/*[text()='Pickup.DateTime']")
    print(p_time[0].tag) # {http://http://www.someaddress.com/someendpoint}FieldName
    

But this gives me the actual element with text `Pickup.DateTime` and I am
interested in getting its parent as shown above.

**As a side note** : it took me almost an hour even to get this far because I
find the lxml documentation to be very cumbersome. If anyone has a link with a
good tutorial please post it at least as a comment. Thanks!


Answer: I have found how to get it:

    
    
    p_time = xml_tree.xpath("//*[local-name()='TxnField']/*[text()='Pickup.DateTime']/./..")
    

