further refactoring of generate_xml