<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_template_as_xml(template_id,**kwargs):
 """ Turn a template into an xml template """

    template_xml = etree.Element("template_definition")

    template_i = db.DBSession.query(Template).filter(
            Template.id==template_id).options(
                #joinedload_all('templatetypes.typeattrs.default_dataset.metadata')
                joinedload('templatetypes').joinedload('typeattrs').joinedload('default_dataset').joinedload('metadata')

            ).one()

    template_name = etree.SubElement(template_xml, "template_name")
    template_name.text = template_i.name
    template_description = etree.SubElement(template_xml, "template_description")
    template_description.text = template_i.description
    resources = etree.SubElement(template_xml, "resources")

    for type_i in template_i.templatetypes:
        xml_resource    = etree.SubElement(resources, "resource")

        resource_type   = etree.SubElement(xml_resource, "type")
        resource_type.text   = type_i.resource_type

        name   = etree.SubElement(xml_resource, "name")
        name.text   = type_i.name

        description   = etree.SubElement(xml_resource, "description")
        description.text   = type_i.description

        alias   = etree.SubElement(xml_resource, "alias")
        alias.text   = type_i.alias

        if type_i.layout is not None and type_i.layout != "":
            layout = _get_layout_as_etree(type_i.layout)
            xml_resource.append(layout)

        for type_attr in type_i.typeattrs:
            attr = _make_attr_element_from_typeattr(xml_resource, type_attr)

        resources.append(xml_resource)

    xml_string = etree.tostring(template_xml, encoding="unicode")

    return xml_string