This should work:
//*[ancestor::foo[bar[@attr="val"]]]
or alternatively
root/foo/bar[ancestor::foo[bar[@attr="val"]]]
matches the second <bar>
element
<root>
<foo>
<bar attr="xxx"></bar>
</foo>
<foo>
<bar attr="val"></bar>
</foo>
<foo>
<bar attr="zzz"></bar>
</foo>
</root>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…