Improve loading of malformed XForms

http://manage.dimagi.com/default.asp?142079
http://manage.dimagi.com/default.asp?142193

Note that the XML in case 142079 is so badly malformed that it still does not load, but many of the loading problems have been solved by this PR.
Move XML to templates (no other changes).