Xpath reference

i want to reference a variable hierarchy ( xpath ) in an error message. how can i do this? e.g.

Doc A
Doc B
var1
var2
Doc C
var3
var4

so in an error message, if the current doc variable is var4, the variable name should appear as DocA.var4 in the error message. IF it is var3 it should appear as DocA.DocB.DocC.var3

than