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.
@emord you don't need to add this function if you rebase #260 on master after this is merged.