Syntax of the xml document:
Z
Z__2
Z__3
>
You already have a good accepted answer, but here are some other helpful expressions:
//z/ancestor::x/@name - Find elements anywhere, then find all the ancestor elements, and then the name="…" attributes of them.
//z/../../@name - Find the elements, and then find the parent node(s) of those, and then the parent node(s) of those, and then the name attribute(s) of the final set.
//z/parent::*/parent::*/@name, where the * means "an element with any name".The // is useful, but inefficient. If you know that the hierarchy is x/y/z, then it is more efficient to do something like //x[y/z]/@name