You can do this by using /parent::node() in the xpath. Simply append /parent::node() to the child elements xpath.
For example: Let xpath of child element is childElementXpath.
Then xpath of its immediate ancestor would be childElementXpath/parent::node().
Xpath of its next ancestor would be childElementXpath/parent::node()/parent::node()
and so on..
Also, you can navigate to an ancestor of an element using
'childElementXpath/ancestor::*[@attr="attr_value"]'
. This would be useful when you have a known child element which is unique but has a parent element which cannot be uniquely identified.