'convert s3ocrxml to dictionary so that it can be used in templates
@param s3ocrxml: content of a s3ocrxml file, in text
@return: equivalent dictionary for s3ocrxml file'

def __s3ocrxml2dict(self, s3ocrxml):

    db = current.db
    s3ocr_etree = etree.fromstring(s3ocrxml)
    s3ocrdict = Storage()
    resource_seq = []
    for resource in s3ocr_etree:
        resourcename = resource.attrib.get('name')
        table = db[resourcename]
        s3ocrdict[resourcename] = Storage()
        resource_seq.append(resourcename)
        field_seq = []
        for field in resource:
            get = field.attrib.get
            fieldname = get('name')
            if ((get('readable') == 'True') and (get('writable') == 'True')):
                field_seq.append(fieldname)
                fieldlabel = get('label')
                fieldtype = get('type')
                numlines = get('lines', '1')
                if (get('reference') == '1'):
                    fieldreference = True
                else:
                    fieldreference = False
                fieldresource = get('resource')
                if (get('has_options') == 'True'):
                    fieldhasoptions = True
                else:
                    fieldhasoptions = False
                fieldcomment = table[fieldname].comment
                if fieldhasoptions:
                    try:
                        s3ocrselect = field.getchildren()[0]
                        options_found = True
                    except IndexError:
                        fieldoptions = None
                        options_found = False
                    if options_found:
                        numoptions = len(s3ocrselect.getchildren())
                        optionlist = []
                        for option in s3ocrselect:
                            optionlabel = option.text
                            optionvalue = option.attrib.get('value')
                            optionlist.append(Storage({'label': optionlabel, 'value': optionvalue}))
                        fieldoptions = Storage({'count': numoptions, 'list': optionlist})
                    else:
                        fieldoptions = None
                else:
                    fieldoptions = None
                s3ocrdict[resourcename][fieldname] = Storage({'label': fieldlabel, 'type': fieldtype, 'comment': fieldcomment, 'reference': fieldreference, 'resource': fieldresource, 'has_options': fieldhasoptions, 'options': fieldoptions, 'lines': int(numlines)})
        s3ocrdict[resourcename]['$field_seq'] = field_seq
    s3ocrdict['$resource_seq'] = resource_seq
    return s3ocrdict
