Ok, that explains the behaviour, the question is what we should do about the, admitingly, weird behaviour.
How it is weird - it's same as in xslt.
One possibilty would be to decide that we want JXTG to be more like a functional language.
+1
In that case we should deprecate jx:set and introduce a jx:let instead that just gives a local name for an expression and that gives an exception if you try to set the "variable" to a new value.
Not sure this is necessary.
I think I would prefer this behaviour as I would prefer having a template language without side effects.
+100
Another possibility is to let set asign the value to the first variable binding with the same name that it finds when the stack is searched,
-1. Don't think this is a possibility; as noted above, templates should be side effect free.
Vadim