I have searched all around to find a solution to my problem, but i just got more questions...
consider the following XML:
What you're attempting isn't directly possible, but why not simply match on the id attribute's value in the first place? If you absolutely need the indirection of the called template, then call it from the template that matches by id (see name4below):
name1
name2
name3
name1
name2
name3
name4
Input:
...
...
...
...
Output:
name1name2name3name4