def render_to_xml(self, context_dict): """ Render the template using the `context_dict` dict. Returns an `etree` XML element. """ # add dummy STATIC_URL to template context context_dict.setdefault("STATIC_URL", "/dummy-static/") try: xml_str = capa_render_template(self.TEMPLATE_NAME, context_dict) except: raise TemplateError(exceptions.text_error_template().render()) # Attempt to construct an XML tree from the template # This makes it easy to use XPath to make assertions, rather # than dealing with a string. # We modify the string slightly by wrapping it in <test> # tags, to ensure it has one root element. try: xml = etree.fromstring("<test>" + xml_str + "</test>") except Exception as exc: raise TemplateError("Could not parse XML from '{0}': {1}".format( xml_str, str(exc))) else: return xml