'Add a <resource> to the element tree
@param record: the record
@param rfields: list of reference fields to export
@param dfields: list of data fields to export
@param parent: the parent element
@param base_url: the base URL of the resource
@param reference_map: the reference map of the request
@param export_map: the export map of the request
@param lazy:
@param components: list of components to include from referenced
resources (tablenames)
@param filters: additional URL filters (Sync), as dict
{tablename: {url_var: string}}
@param msince: the minimum update datetime for exported records
@param master: True of this is the master resource
@param target: alias of component targetted (or None to target master resource)
@param location_data: the location_data for GIS encoding
@param xmlformat:'

def __export_resource(self, record, rfields=[], dfields=[], parent=None, base_url=None, reference_map=None, export_map=None, lazy=None, components=None, filters=None, msince=None, master=True, target=None, location_data=None, xmlformat=None):

    pkey = self.table._id
    if base_url:
        record_url = ('%s/%s' % (base_url, record[pkey]))
    else:
        record_url = None
    xml = current.xml
    MTIME = xml.MTIME
    MCI = xml.MCI
    export = self._export_record
    (element, rmap) = self._export_record(record, rfields=rfields, dfields=dfields, parent=parent, export_map=export_map, lazy=lazy, url=record_url, master=master, location_data=location_data)
    if (element is None):
        return None
    add = True
    if msince:
        mtime = record.get(MTIME)
        if (mtime and (mtime < msince)):
            add = False
    if components:
        get_hierarchy_link = current.s3db.hierarchy_link
        resource_components = self.components
        for alias in components:
            component = resource_components.get(alias)
            if (not component):
                continue
            hierarchy_link = get_hierarchy_link(component.tablename)
            if (component.link is not None):
                c = component.link
                calias = None
                lalias = c.alias
            else:
                c = component
                calias = c.alias
                lalias = None
            ctablename = c.tablename
            cpkey = c.table._id
            if (c._rows is None):
                ctable = c.table
                cfields = ctable.fields
                if (xml.filter_mci and (xml.MCI in cfields)):
                    mci_filter = (FS(MCI) >= 0)
                    c.add_filter(mci_filter)
                if (filters and (ctablename in filters)):
                    queries = S3URLQuery.parse(self, filters[ctablename])
                    [c.add_filter(q) for a in queries for q in queries[a]]
                if (msince and ((alias != hierarchy_link) or add) and (MTIME in cfields)):
                    query = (FS(MTIME) > msince)
                    c.add_filter(query)
                if export_map:
                    export_list = export_map.get(ctablename)
                    if export_list:
                        query = (~ FS(cpkey.name).belongs(export_list))
                        c.add_filter(query)
                if xmlformat:
                    (include, exclude) = xmlformat.get_fields(ctablename)
                else:
                    (include, exclude) = (None, None)
                c.load(fields=include, skip=exclude, limit=None, virtual=False, cacheable=True)
            crecords = self.get(record[pkey], component=calias, link=lalias)
            if (not crecords):
                continue
            else:
                add = True
            (crfields, cdfields) = c.split_fields(skip=[c.fkey])
            if ((not c.multiple) and len(crecords)):
                crecords = [crecords[0]]
            if (target == ctablename):
                master = True
                if (not location_data):
                    count = c.count()
                    location_data = (current.gis.get_location_data(c, count=count) or {})
            else:
                master = False
            if record_url:
                cname = (c.name if c.linked else c.alias)
                component_url = ('%s/%s' % (record_url, cname))
            else:
                component_url = None
            export = c._export_record
            map_record = c.__map_record
            for crecord in crecords:
                if component_url:
                    crecord_url = ('%s/%s' % (component_url, crecord[cpkey]))
                else:
                    crecord_url = None
                (celement, crmap) = export(crecord, rfields=crfields, dfields=cdfields, parent=element, lazy=lazy, url=crecord_url, master=master, location_data=location_data)
                if ((not self.muntil) or (c.muntil and (c.muntil > self.muntil))):
                    self.muntil = c.muntil
                map_record(crecord, crmap, reference_map, export_map)
    if add:
        self.__map_record(record, rmap, reference_map, export_map)
    elif ((parent is not None) and (element is not None)):
        try:
            parent.remove(element)
        except ValueError:
            pass
        element = None
    return element
