I don't get why you have an intermediate XML phase; seems like it offers nothing beyond what a custom parse tree would except a lot of pain and headache.
FWIW, Template Toolkit has had good success with "compiling" templates to perl subs, then caching those subs (in memory, not on disk).