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>