docs: def get_network_as_xml_template(network_id,**kwargs):
      """
          Turn an existing network into an xml template
          using its attributes.
          If an optional scenario ID is passed in, default
          values will be populated from that scenario.
      """
      template_xml = etree.Element("template_definition")
  
      net_i = db.DBSession.query(Network).filter(Network.id==network_id).one()
  
      template_name = etree.SubElement(template_xml, "template_name")
      template_name.text = "TemplateType from Network %s"%(net_i.name)
      layout = _get_layout_as_etree(net_i.layout)
  
      resources = etree.SubElement(template_xml, "resources")
      if net_i.attributes:
          net_resource    = etree.SubElement(resources, "resource")
  
          resource_type   = etree.SubElement(net_resource, "type")
          resource_type.text   = "NETWORK"
  
          resource_name   = etree.SubElement(net_resource, "name")
          resource_name.text   = net_i.name
  
          layout = _get_layout_as_etree(net_i.layout)
          if layout is not None:
              net_resource.append(layout)
  
          for net_attr in net_i.attributes:
              _make_attr_element_from_resourceattr(net_resource, net_attr)
  
          resources.append(net_resource)
  
      existing_types = {'NODE': [], 'LINK': [], 'GROUP': []}
      for node_i in net_i.nodes:
          node_attributes = node_i.attributes
          attr_ids = [res_attr.attr_id for res_attr in node_attributes]
          if len(attr_ids) > 0 and attr_ids not in existing_types['NODE']:
  
              node_resource    = etree.Element("resource")
  
              resource_type   = etree.SubElement(node_resource, "type")
              resource_type.text   = "NODE"
  
              resource_name   = etree.SubElement(node_resource, "name")
              resource_name.text   = node_i.node_name
  
              layout = _get_layout_as_etree(node_i.layout)
  
              if layout is not None:
                  node_resource.append(layout)
  
              for node_attr in node_attributes:
                  _make_attr_element_from_resourceattr(node_resource, node_attr)
  
              existing_types['NODE'].append(attr_ids)
              resources.append(node_resource)
  
      for link_i in net_i.links:
          link_attributes = link_i.attributes
          attr_ids = [link_attr.attr_id for link_attr in link_attributes]
          if len(attr_ids) > 0 and attr_ids not in existing_types['LINK']:
              link_resource    = etree.Element("resource")
  
              resource_type   = etree.SubElement(link_resource, "type")
              resource_type.text   = "LINK"
  
              resource_name   = etree.SubElement(link_resource, "name")
              resource_name.text   = link_i.link_name
  
              layout = _get_layout_as_etree(link_i.layout)
  
              if layout is not None:
                  link_resource.append(layout)
  
              for link_attr in link_attributes:
                  _make_attr_element_from_resourceattr(link_resource, link_attr)
  
              existing_types['LINK'].append(attr_ids)
              resources.append(link_resource)
  
      for group_i in net_i.resourcegroups:
          group_attributes = group_i.attributes
          attr_ids = [group_attr.attr_id for group_attr in group_attributes]
          if len(attr_ids) > 0 and attr_ids not in existing_types['GROUP']:
              group_resource    = etree.Element("resource")
  
              resource_type   = etree.SubElement(group_resource, "type")
              resource_type.text   = "GROUP"
  
              resource_name   = etree.SubElement(group_resource, "name")
              resource_name.text   = group_i.group_name
  
  
              for group_attr in group_attributes:
                  _make_attr_element_from_resourceattr(group_resource, group_attr)
  
              existing_types['GROUP'].append(attr_ids)
              resources.append(group_resource)
  
      xml_string = etree.tostring(template_xml, encoding="unicode")
  
      return xml_string