'Get fields in a table as <fields> element
@param prefix: the application prefix
@param name: the resource name (without prefix)
@param parent: the parent element to append the tree to
@param options: include option lists in option fields
@param references: include option lists even in reference fields'

def get_fields(self, prefix, name, parent=None, meta=False, options=False, references=False, labels=False):

    db = current.db
    tablename = ('%s_%s' % (prefix, name))
    table = db.get(tablename, None)
    if (parent is not None):
        fields = parent
    else:
        fields = etree.Element(self.TAG.fields)
    if table:
        ATTRIBUTE = self.ATTRIBUTE
        if (parent is None):
            fields.set(ATTRIBUTE.resource, tablename)
        for f in table.fields:
            ftype = str(table[f].type)
            if (ftype[:9] == 'reference'):
                ktablename = ftype[10:]
                s = current.s3db.get_config(tablename, 'super_entity')
                if (not isinstance(s, (list, tuple))):
                    s = [s]
                if (ktablename in s):
                    continue
            if ((f in self.IGNORE_FIELDS) or (ftype == 'id')):
                continue
            if (f in self.FIELDS_TO_ATTRIBUTES):
                if (not meta):
                    continue
                tag = self.TAG.meta
            else:
                tag = self.TAG.field
            readable = table[f].readable
            writable = table[f].writable
            field = etree.SubElement(fields, tag)
            if options:
                p = field
                if ((not references) and (ftype[:9] in ('reference', 'list:refe'))):
                    p = None
            else:
                p = None
            opts = self.get_field_options(table, f, parent=p)
            set_attribute = field.set
            set_attribute(ATTRIBUTE.name, f)
            set_attribute(ATTRIBUTE.type, ftype)
            set_attribute(ATTRIBUTE.readable, str(readable))
            set_attribute(ATTRIBUTE.writable, str(writable))
            has_options = str((((opts is not None) and len(opts) and True) or False))
            set_attribute(ATTRIBUTE.has_options, has_options)
            if labels:
                label = s3_unicode(table[f].label)
                set_attribute(ATTRIBUTE.label, label)
                comment = table[f].comment
                if comment:
                    comment = s3_unicode(comment)
                if (comment and ('<' in comment)):
                    comment = s3_strip_markup(comment)
                if comment:
                    set_attribute(ATTRIBUTE.comment, comment)
    return fields
