def loadFromExcel(cntlr, modelXbrl, excelFile, mappedUri):
    from openpyxl import load_workbook
    from arelle import ModelDocument, ModelXbrl, XmlUtil
    from arelle.ModelDocument import ModelDocumentReference
    from arelle.ModelValue import qname

    def xlUnicodeChar(match):
        return chr(int(match.group(1), 16))

    def xlValue(cell):
        v = cell.value
        if isinstance(v, str):
            return xlUnicodePattern.sub(xlUnicodeChar, v).replace('\r\n', '\n').replace('\r', '\n')
        return v
    defaultLabelLang = (saveXmlLang or 'en')
    importColumnHeaders = {'名前空間プレフィックス': 'prefix', 'prefix': 'prefix', '要素名': 'name', 'name': 'name', 'type': 'type', 'typePrefix': 'typePrefix', 'substitutionGroup': 'substitutionGroup', 'periodType': 'periodType', 'balance': 'balance', 'abstract': 'abstract', 'nillable': 'nillable', 'depth': 'depth', 'minLength': 'minLength', 'maxLength': 'maxLength', 'minInclusive': 'minInclusive', 'maxInclusive': 'maxInclusive', 'length': 'length', 'fixed': 'fixed', 'pattern': 'pattern', 'enumeration': 'enumeration', 'excludedEnumeration': 'excludedEnumeration', 'preferred label': 'preferredLabel', 'preferredLabel': 'preferredLabel', 'presentation parent': 'presentationParent', 'calculation parent': 'calculationParent', 'calculation weight': 'calculationWeight', '標準ラベル（日本語）': ('label', XbrlConst.standardLabel, 'ja', 'indented'), '冗長ラベル（日本語）': ('label', XbrlConst.verboseLabel, 'ja'), '標準ラベル（英語）': ('label', XbrlConst.standardLabel, 'en'), '冗長ラベル（英語）': ('label', XbrlConst.verboseLabel, 'en'), '用途区分、財務諸表区分及び業種区分のラベル（日本語）': ('labels', XbrlConst.standardLabel, 'ja'), '用途区分、財務諸表区分及び業種区分のラベル（英語）': ('labels', XbrlConst.standardLabel, 'en'), 'label': ('label', XbrlConst.standardLabel, defaultLabelLang, 'indented'), 'label, standard': ('label', XbrlConst.standardLabel, defaultLabelLang, 'overridePreferred'), 'label, terse': ('label', XbrlConst.terseLabel, defaultLabelLang), 'label, verbose': ('label', XbrlConst.verboseLabel, defaultLabelLang), 'label, documentation': ('label', XbrlConst.documentationLabel, defaultLabelLang), 'group': 'linkrole', 'linkrole': 'linkrole', 'ELR': 'linkrole', 'dimension default': 'dimensionDefault'}
    fatalLoadingErrors = []
    startedAt = time.time()
    if os.path.isabs(excelFile):
        priorCWD = os.getcwd()
        os.chdir(os.path.dirname(excelFile))
    else:
        priorCWD = None
    importExcelBook = load_workbook(excelFile, data_only=True)
    sheetNames = importExcelBook.get_sheet_names()
    dtsSheet = None
    if ('XBRL DTS' in sheetNames):
        dtsSheet = 'XBRL DTS'
    elif ('DTS' in sheetNames):
        dtsSheet = 'DTS'
    elif ('Sheet2' in sheetNames):
        dtsSheet = 'Sheet2'
    if dtsSheet:
        dtsWs = importExcelBook[dtsSheet]
    else:
        dtsWs = None
    imports = {'xbrli': (('namespace', XbrlConst.xbrli), ('schemaLocation', 'http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd'))}
    importXmlns = {}
    hasPreLB = hasCalLB = hasDefLB = hasRefLB = hasGenLB = False
    preLB = []
    defLB = []
    calLB = []
    refLB = []
    genLB = []

    def lbDepthList(lbStruct, depth, parentList=None):
        if (len(lbStruct) > 0):
            if ((depth == topDepth) or (not hasDepthColumn)):
                return lbStruct[(- 1)].childStruct
            return lbDepthList(lbStruct[(- 1)].childStruct, (depth - 1), list)
        else:
            if hasDepthColumn:
                cntlr.addToLog('Depth error, Excel sheet: {excelSheet} row: {excelRow}'.format(excelSheet=importSheetName, excelRow=iRow), messageCode='importExcel:depth')
            return None
    splitString = None
    importFileName = None
    importSheetNames = []
    skipRows = []
    genDocs = {}
    genElementsDoc = None

    def newDoc(name):
        genDocs[name] = PythonUtil.attrdict(name=name, initialComment=None, schemaDocumentation=None, extensionSchemaPrefix='', extensionSchemaFilename='', extensionSchemaRelDirname=None, extensionSchemaNamespaceURI='', extensionSchemaVersion=None, extensionRoles={}, extensionRoleLabels=defaultdict(set), extensionElements={}, extensionTypes={}, extensionLabels={}, extensionReferences=OrderedDefaultDict(OrderedSet), hasEnumerationDocumentation=False, imports={'xbrli': (('namespace', XbrlConst.xbrli), ('schemaLocation', 'http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd'))}, includes=[], importXmlns={}, importFilenames={}, childGenDocs=[], linkbaseRefs=[], labelLinkbases=[], referenceLinkbases=[], hasPreLB=False, hasCalLB=False, hasDefLB=False, hasRefLB=False, hasGenLB=False, generated=False)
        return genDocs[name]
    thisDoc = newDoc(None)
    excelDir = (os.path.dirname(excelFile) + os.path.sep)

    def docRelpath(filename, baseDir=None):
        if (baseDir is None):
            baseDir = thisDoc.extensionSchemaRelDirname
        if ((baseDir is not None) and (not (UrlUtil.isAbsolute(filename) or os.path.isabs(filename)))):
            return posixpath.relpath(filename, baseDir)
        return filename
    isUSGAAP = False
    isGenerateAndImport = True
    extensionPrefixForCoreLabels = None
    dtsActionColIndex = 0
    dtsFiletypeColIndex = 1
    dtsPrefixColIndex = 2
    dtsFilenameColIndex = 3
    dtsNamespaceURIColIndex = 4
    for (iRow, row) in enumerate((dtsWs.rows if dtsWs else ())):
        try:
            if (len(row) < 1):
                continue
            _col0 = row[0].value
            if (isinstance(_col0, str) and _col0.startswith('#')):
                continue
            if (iRow == 0):
                for (i, col) in enumerate(row):
                    v = xlValue(col)
                    if isinstance(v, str):
                        if (v == 'specification'):
                            dtsActionColIndex = i
                        if v.startswith('file type'):
                            dtsFiletypeColIndex = i
                        if v.startswith('prefix'):
                            dtsPrefixColIndex = i
                        if v.startswith('file, href or role definition'):
                            dtsFilenameColIndex = i
                        if v.startswith('namespace URI'):
                            dtsNamespaceURIColIndex = i
                continue
            action = filetype = prefix = filename = namespaceURI = None
            if (len(row) > dtsActionColIndex):
                action = xlValue(row[dtsActionColIndex])
            if (len(row) > dtsFiletypeColIndex):
                filetype = xlValue(row[dtsFiletypeColIndex])
            if (len(row) > dtsPrefixColIndex):
                prefix = xlValue(row[dtsPrefixColIndex])
            if (len(row) > dtsFilenameColIndex):
                filename = xlValue(row[dtsFilenameColIndex])
            if (len(row) > dtsNamespaceURIColIndex):
                namespaceURI = xlValue(row[dtsNamespaceURIColIndex])
            lbType = lang = None
            if (action == 'import'):
                if (filetype in ('role', 'arcrole')):
                    continue
                elif (filetype == 'schema'):
                    thisDoc.imports[prefix] = (('namespace', namespaceURI), ('schemaLocation', docRelpath(filename)))
                    thisDoc.importXmlns[prefix] = namespaceURI
                    thisDoc.importFilenames[prefix] = filename
                    if re.match('http://[^/]+/us-gaap/', namespaceURI):
                        isUSGAAP = True
                elif (filetype == 'linkbase'):
                    typeLang = prefix.split()
                    if (len(typeLang) > 0):
                        lbType = typeLang[0]
                    else:
                        lbType = 'unknown'
                    thisDoc.linkbaseRefs.append((lbType, filename, False))
            elif ((action == 'include') and filename):
                thisDoc.includes.append(docRelpath(filename))
            elif ((action == 'xmlns') and prefix and namespaceURI):
                thisDoc.importXmlns[prefix] = namespaceURI
            elif (action in ('extension', 'generate')):
                if (filetype == 'schema'):
                    if prefix:
                        if (not thisDoc.name):
                            del genDocs[thisDoc.name]
                        thisDoc = newDoc(prefix)
                    thisDoc.extensionSchemaPrefix = prefix
                    thisDoc.extensionSchemaFilename = filename
                    thisDoc.extensionSchemaNamespaceURI = namespaceURI
                    if ((not UrlUtil.isAbsolute(filename)) and (not os.path.isabs(filename))):
                        thisDoc.extensionSchemaRelDirname = posixpath.dirname(filename)
                    else:
                        thisDoc.extensionSchemaRelDirname = None
                elif (filetype == 'linkbase'):
                    typeLang = prefix.split()
                    if (len(typeLang) > 0):
                        lbType = typeLang[0]
                    else:
                        lbType = 'unknown'
                    if (len(typeLang) > 1):
                        lang = referenceRole = typeLang[1]
                    else:
                        lang = None
                        referenceRole = XbrlConst.standardReference
                    if (lbType in ('label', 'generic-label')):
                        thisDoc.labelLinkbases.append((lbType, lang, filename))
                        if ((action == 'extension') and (not extensionPrefixForCoreLabels)):
                            extensionPrefixForCoreLabels = thisDoc.extensionSchemaPrefix
                    elif (lbType in ('reference', 'generic-reference')):
                        hasRefLB = True
                        thisDoc.referenceLinkbases.append((lbType, referenceRole, filename))
                    elif (lbType == 'presentation'):
                        thisDoc.hasPreLB = hasPreLB = True
                    elif (lbType == 'definition'):
                        thisDoc.hasDefLB = hasDefLB = True
                    elif (lbType == 'calculation'):
                        thisDoc.hasCalLB = hasCalLB = True
                    elif (lbType == 'generic'):
                        thisDoc.hasGenLB = hasGenLB = True
                    thisDoc.linkbaseRefs.append((lbType, filename, True))
                elif ((filetype == 'initialComment') and prefix):
                    thisDoc.initialComment = prefix
                elif ((filetype == 'schemaDocumentation') and prefix):
                    thisDoc.schemaDocumentation = prefix
                elif (filetype == 'enumerationDocumentation'):
                    thisDoc.hasEnumerationDocumentation = True
                elif ((filetype == 'role') and namespaceURI):
                    thisDoc.extensionRoles[namespaceURI] = (filename, prefix)
                elif ((filetype == 'role label') and namespaceURI and prefix):
                    thisDoc.extensionRoleLabels[namespaceURI].add((filename, prefix))
                elif ((filetype == 'schema-version') and filename):
                    thisDoc.extensionSchemaVersion = filename
                elif ((filetype == 'table-style') and (filename == 'xbrl-us')):
                    isUSGAAP = True
                elif (filetype == 'elements'):
                    genElementsDoc = thisDoc
            elif ((action == 'meta') and (filetype == 'table-style') and (filename == 'xbrl-us')):
                isUSGAAP = True
            elif ((action == 'meta') and (filetype == 'generate-style') and (filename == 'import-separately')):
                isGenerateAndImport = False
            elif ((action == 'workbook') and filename):
                importFileName = filename
            elif ((action == 'worksheet') and filename):
                importSheetNames.append(filename)
            elif ((action == 'colheader') and filename and namespaceURI):
                if (namespaceURI == 'split'):
                    splitString = filename
                else:
                    importColHeaderMap[filename].append(namespaceURI)
                    if (namespaceURI not in importColumnHeaders):
                        fatalLoadingErrors.append('colheader {} definition {} not recognized.'.format(filename, namespaceURI))
            elif ((action == 'skip rows') and filename):
                (fromRow, _sep, toRow) = filename.partition('-')
                try:
                    skipRows.append((int(fromRow), (int(toRow) if toRow else int(fromRow))))
                except (ValueError, TypeError):
                    fatalLoadingErrors.append('Exception (at skip rows): {error}, Excel sheet: {excelSheet} row: {excelRow}'.format(error=err, excelSheet=dtsSheet, excelRow=iRow))
        except Exception as err:
            fatalLoadingErrors.append('Exception: {error}, Excel sheet: {excelSheet} row: {excelRow}, Traceback: {traceback}'.format(error=err, excelSheet=dtsSheet, excelRow=iRow, traceback=traceback.format_tb(sys.exc_info()[2])))
    for thisDoc in genDocs.values():
        linkbaseRefsToRemove = [i for (i, (lbType, filename, generate)) in enumerate(thisDoc.linkbaseRefs) if ((not generate) and ((lbType, filename, True) in thisDoc.linkbaseRefs))]
        while len(linkbaseRefsToRemove):
            i = linkbaseRefsToRemove.pop()
            thisDoc.linkbaseRefs.pop(i)
    dtsWs = None
    genOrder = []
    for (name, doc) in genDocs.items():
        insertPos = len(genOrder)
        for (i, otherDoc) in enumerate(genOrder):
            if (doc.name in otherDoc.imports):
                insertPos = i
                break
        genOrder.insert(insertPos, doc)
    if importFileName:
        importExcelBook = load_workbook(importFileName, read_only=True, data_only=True)
        sheetNames = importExcelBook.get_sheet_names()
    if importSheetNames:
        for importSheetName in importSheetNames:
            if (importSheetName not in sheetNames):
                fatalLoadingErrors.append('Worksheet {} specified for Excel importing, but not present in workbook.'.format(importSheetName))
    else:
        for s in sheetNames:
            if s.endswith('Concepts'):
                importSheetNames.append(s)
        if (not importSheetNames):
            for s in sheetNames:
                if (('xbrl' in s.lower()) and ('dts' not in s)):
                    importSheetNames.append(s)
        if (not importSheetNames):
            fatalLoadingErrors.append('Worksheet {} specified for Excel importing, but not present in workbook.'.format(importSheetName))
    if ((not isUSGAAP) and genOrder):
        genOrder[0].importXmlns['iod'] = 'http://disclosure.edinet-fsa.go.jp/taxonomy/common/2013-03-31/iod'
    headerCols = OrderedDict()
    headerColsAllElrs = set()
    hasLinkroleSeparateRow = True
    hasPreferredLabelTextColumn = False
    hasConceptAttributeColumn = False
    hasDepthColumn = False
    hasPresentationParentColumn = False
    hasRelationshipToCol = False
    hasrelationshipAttributeColumn = False
    headerRows = set()
    topDepth = 999999
    for importSheetName in importSheetNames:
        if (importSheetName not in sheetNames):
            continue
        headerCols.clear()
        headerRows.clear()
        hasConceptAttributeColumn = False
        hasDepthColumn = False
        hasPresentationParentColumn = False
        hasRelationshipToCol = False
        hasrelationshipAttributeColumn = False
        conceptsWs = importExcelBook[importSheetName]

        def setHeaderCols(row):
            headerCols.clear()
            for (iCol, colCell) in enumerate(row):
                v = xlValue(colCell)
                if isinstance(v, str):
                    v = v.strip()
                if (v in importColHeaderMap):
                    for hdr in importColHeaderMap[v]:
                        if (hdr in importColumnHeaders):
                            headerCols[importColumnHeaders[hdr]] = iCol
                elif (v in importColumnHeaders):
                    headerCols[importColumnHeaders[v]] = iCol
                elif isinstance(v, str):
                    if any((v.startswith(r) for r in ('label,', 'labels,', 'reference,', 'references,', 'relationship to,'))):
                        m = resourceParsePattern.match(v)
                        if m:
                            _resourceType = m.group(1)
                            _resourceRole = ('/' + m.group(2))
                            _resourceLangOrPart = m.group(4)
                            headerCols[(_resourceType, _resourceRole, _resourceLangOrPart)] = iCol
                    else:
                        headerCols[v] = iCol
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            if any(((fromRow <= (iRow + 1) <= toRow) for (fromRow, toRow) in skipRows)):
                continue
            setHeaderCols(row)
            if ((sum((1 for h in headerCols if (h in ('name', 'type', 'depth', 'periodType')))) >= 3) or (sum((1 for h in headerCols if ((h == 'name') or (isinstance(h, tuple) and (h[0] == 'relationship to'))))) >= 2)):
                headerRows.add((iRow + 1))
            if ('linkrole' in headerCols):
                hasLinkroleSeparateRow = False
            if (('preferredLabel' in headerCols) and any(((isinstance(h, tuple) and (h[0] == 'label') and (h[1] == '/preferredLabel')) for h in headerCols))):
                hasPreferredLabelTextColumn = True
            if ('depth' in headerCols):
                hasDepthColumn = True
            if ('presentationParent' in headerCols):
                hasPresentationParentColumn = True
            if ((not hasDepthColumn) and hasPresentationParentColumn):
                topDepth = 0
            hasRelationshipToCol = any(((h[0] == 'relationship to') for h in headerCols if isinstance(h, tuple)))
            headerCols.clear()

        def cellHasValue(row, header, _type):
            if (header in headerCols):
                iCol = headerCols[header]
                return ((iCol < len(row)) and isinstance(row[iCol].value, _type))
            return False

        def cellValue(row, header, strip=False, nameChars=False, default=None):
            if (header in headerCols):
                iCol = headerCols[header]
                if (iCol < len(row)):
                    v = xlValue(row[iCol])
                    if (strip and isinstance(v, str)):
                        v = v.strip()
                    if (nameChars and isinstance(v, str)):
                        v = ''.join((c for c in v if (c.isalnum() or (c in ('.', '_', '-')))))
                    if (v is None):
                        return default
                    return v
            return default

        def valueNameChars(v):
            return ''.join((c for c in v if (c.isalnum() or (c in ('.', '_', '-')))))

        def rowPrefixNameValues(row):
            prefix = cellValue(row, 'prefix', nameChars=True)
            if cellHasValue(row, 'name', str):
                if (not prefix):
                    (prefix, _sep, _name) = cellValue(row, 'name').partition(':')
                    if (not _sep):
                        prefix = ''
                    name = cellValue(row, 'name', nameChars=True)[len(prefix):]
                else:
                    name = cellValue(row, 'name', nameChars=True)
            else:
                name = None
            if ((not prefix) and ('prefix' not in headerCols) and (genElementsDoc is not None)):
                prefix = genElementsDoc.extensionSchemaPrefix
            return (prefix, name)

        def checkImport(thisDoc, qname):
            (prefix, sep, localName) = qname.partition(':')
            if sep:
                if (prefix not in thisDoc.imports):
                    if (prefix == 'xbrldt'):
                        thisDoc.imports['xbrldt'] = (('namespace', XbrlConst.xbrldt), ('schemaLocation', 'http://www.xbrl.org/2005/xbrldt-2005.xsd'))
                    elif (prefix == 'nonnum'):
                        thisDoc.imports['nonnum'] = (('namespace', 'http://www.xbrl.org/dtr/type/non-numeric'), ('schemaLocation', 'http://www.xbrl.org/dtr/type/nonNumeric-2009-12-16.xsd'))
                    elif ((prefix != thisDoc.extensionSchemaPrefix) and (prefix != 'xs')):
                        cntlr.addToLog('Warning: prefix schema file is not imported for: {qname}'.format(qname=qname), messageCode='importExcel:warning', file=thisDoc.extensionSchemaFilename)
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            if ((iRow + 1) in headerRows):
                setHeaderCols(row)
                hasConceptAttributeColumn = any((v.startswith('attribute, ') for v in headerCols if isinstance(v, str)))
                hasRelationshipAttributeColumn = any((v.startswith('relationship attribute, ') for v in headerCols if isinstance(v, str)))
            elif ((not (hasLinkroleSeparateRow and ((iRow + 1) in headerRows))) and ('depth' in headerCols)):
                depth = cellValue(row, 'depth')
                if (isinstance(depth, int) and (depth < topDepth)):
                    topDepth = depth
        currentELR = currentELRdefinition = None
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            useLabels = False
            eltEnumRefsParts = None
            if any(((fromRow <= (iRow + 1) <= toRow) for (fromRow, toRow) in skipRows)):
                continue
            if (all(((col.value is None) for col in row)) or all(((isinstance(row[i].value, str) and (row[i].value.strip() == 'n/a')) for i in (headerCols.get('name'), headerCols.get('type'), headerCols.get('value')) if (i is not None)))):
                continue
            try:
                isHeaderRow = ((iRow + 1) in headerRows)
                isELRrow = (hasLinkroleSeparateRow and ((iRow + 2) in headerRows))
                if isHeaderRow:
                    setHeaderCols(row)
                    headerColsAllElrs |= _DICT_SET(headerCols.keys())
                elif isELRrow:
                    currentELR = currentELRdefinition = None
                    for colCell in row:
                        v = str((xlValue(colCell) or ''))
                        if v.startswith('http://'):
                            currentELR = v
                        elif ((not currentELRdefinition) and v.endswith('\u3000科目一覧')):
                            currentELRdefinition = v[0:(- 5)]
                        elif (not currentELRdefinition):
                            currentELRdefinition = v
                    if (currentELR or currentELRdefinition):
                        if hasPreLB:
                            preLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                            if hasPresentationParentColumn:
                                preRels = set()
                        if hasDefLB:
                            defLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                        if hasCalLB:
                            calLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                            calRels = set()
                        if hasGenLB:
                            genLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                elif headerCols:
                    if (('linkrole' in headerCols) and cellHasValue(row, 'linkrole', str)):
                        v = cellValue(row, 'linkrole', strip=True)
                        _trialELR = _trialELRdefinition = None
                        if v.startswith('http://'):
                            _trialELR = v
                        elif v.endswith('\u3000科目一覧'):
                            _trialELRdefinition = v[0:(- 5)]
                        else:
                            _trialELRdefinition = v
                        if ((_trialELR and (_trialELR != currentELR)) or (_trialELRdefinition and (_trialELRdefinition != currentELRdefinition))):
                            currentELR = _trialELR
                            currentELRdefinition = _trialELRdefinition
                            if (currentELR or currentELRdefinition):
                                if hasPreLB:
                                    preLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                if hasDefLB:
                                    defLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                if hasCalLB:
                                    calLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                    calRels = set()
                                if hasGenLB:
                                    genLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                    (prefix, name) = rowPrefixNameValues(row)
                    if cellHasValue(row, 'depth', int):
                        depth = cellValue(row, 'depth')
                    elif hasDepthColumn:
                        depth = None
                    else:
                        depth = 0
                    subsGrp = cellValue(row, 'substitutionGroup')
                    isConcept = (subsGrp in ('xbrli:item', 'xbrli:tuple', 'xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))
                    if ((prefix in genDocs) and (name not in genDocs[prefix].extensionElements) and name):
                        thisDoc = genDocs[prefix]
                        eltType = cellValue(row, 'type')
                        eltTypePrefix = cellValue(row, 'typePrefix')
                        if (not eltType):
                            eltType = 'xbrli:stringItemType'
                        elif (eltTypePrefix and (':' not in eltType)):
                            eltType = ((eltTypePrefix + ':') + eltType)
                        elif ((':' not in eltType) and eltType.endswith('ItemType')):
                            eltType = ('xbrli:' + eltType)
                        abstract = cellValue(row, 'abstract')
                        nillable = cellValue(row, 'nillable')
                        balance = cellValue(row, 'balance')
                        periodType = cellValue(row, 'periodType')
                        eltAttrs = {'name': name, 'id': (((prefix or '') + '_') + name)}
                        if eltType:
                            eltAttrs['type'] = eltType
                            checkImport(thisDoc, eltType)
                        if subsGrp:
                            eltAttrs['substitutionGroup'] = subsGrp
                            checkImport(thisDoc, subsGrp)
                        if (abstract or (subsGrp in ('xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))):
                            eltAttrs['abstract'] = (abstract or 'true')
                        if nillable:
                            eltAttrs['nillable'] = nillable
                        if balance:
                            eltAttrs['{http://www.xbrl.org/2003/instance}balance'] = balance
                        if periodType:
                            eltAttrs['{http://www.xbrl.org/2003/instance}periodType'] = periodType
                        if hasConceptAttributeColumn:
                            for header in headerCols:
                                if (isinstance(header, str) and header.startswith('attribute, ')):
                                    value = cellValue(row, header)
                                    if (value not in (None, '')):
                                        eltAttrs[header[11:]] = value
                        eltFacets = None
                        eltEnumRefParts = None
                        if (eltType not in ('nonnum:domainItemType', 'xbrli:booleanItemType', 'xbrli:positiveIntegerItemType', 'xbrli:dateItemType', 'xbrli:gYearItemType')):
                            for facet in ('minLength', 'maxLength', 'minInclusive', 'maxInclusive', 'length', 'fixed', 'pattern', 'enumeration', 'excludedEnumeration'):
                                v = cellValue(row, facet)
                                if (v is not None):
                                    if ((facet == 'enumeration') and v.startswith('See tab ')):
                                        _match = re.match('See tab ([^!]+)([!]([0-9]+):([0-9]+))?', v)
                                        if _match:
                                            (_tab, _dummy, _rowFrom, _rowTo) = _match.groups()
                                            if (_tab in sheetNames):
                                                enumWs = importExcelBook[_tab]
                                                if (_rowFrom and _rowTo):
                                                    colHdrs = [enumWs.cell(row=1, column=i).value for i in range(1, (enumWs.max_column + 1))]
                                                    eltEnumValues = []
                                                    eltEnumRefsParts = []
                                                    for i in range(int(_rowFrom), (int(_rowTo) + 1)):
                                                        _parts = []
                                                        eltEnumRefsParts.append(_parts)
                                                        for (j, h) in enumerate(colHdrs):
                                                            c = enumWs.cell(row=i, column=(j + 1)).value
                                                            if (c is not None):
                                                                if (h == 'enumeration'):
                                                                    eltEnumValues.append(str(c))
                                                                else:
                                                                    m = resourceParsePattern.match(h)
                                                                    if m:
                                                                        _resourceType = m.group(1)
                                                                        _resourceRole = ('/' + m.group(2))
                                                                        _resourceLangOrPart = m.group(4)
                                                                        _parts.append(((_resourceType, _resourceRole, _resourceLangOrPart), c))
                                                    v = ('\n'.join(eltEnumValues) if eltEnumValues else None)
                                                else:
                                                    v = '\n'.join((' = '.join((xlValue(col) for col in row if xlValue(col))) for (i, row) in enumerate(enumWs.rows) if (i > 0)))
                                    if (v is not None):
                                        if (eltFacets is None):
                                            eltFacets = {}
                                        eltFacets[facet] = v
                        if (eltType and (':' in eltType)):
                            (_typePrefix, _sep, _typeName) = eltType.rpartition(':')
                            baseType = cellValue(row, 'baseType')
                            baseTypePrefix = cellValue(row, 'baseTypePrefix')
                            if (baseType and baseTypePrefix):
                                _baseType = '{}:{}'.format(baseTypePrefix, baseType)
                            elif baseType:
                                _baseType = baseType
                            elif _typeName.endswith('ItemType'):
                                _baseType = 'xbrli:tokenItemType'
                            else:
                                _baseType = 'xs:token'
                            if (_typePrefix in genDocs):
                                _typeDoc = genDocs[_typePrefix]
                                if (_typeName not in _typeDoc.extensionTypes):
                                    _typeDoc.extensionTypes[_typeName] = ({'name': _typeName, 'base': _baseType}, eltFacets)
                                thisDoc.extensionElements[name] = (eltAttrs, None)
                            else:
                                thisDoc.extensionElements[name] = (eltAttrs, eltFacets)
                        else:
                            thisDoc.extensionElements[name] = (eltAttrs, eltFacets)
                        thisDoc = None
                    useLabels = True
                    if ((depth is not None) or hasPresentationParentColumn):
                        if (name is None):
                            _label = None
                            for colCell in row:
                                if (colCell.value is not None):
                                    _label = xlValue(colCell)
                                    break
                            print('Sheet {} row {} has relationships and no "name" field, label: {}'.format(importSheetName, (iRow + 1), _label))
                        if hasPreLB:
                            preferredLabel = cellValue(row, 'preferredLabel')
                            if hasDepthColumn:
                                entryList = lbDepthList(preLB, depth)
                                if ((entryList is not None) and isConcept):
                                    if ((not name) or (not prefix)):
                                        _name = 'none'
                                    if (depth == topDepth):
                                        entryList.append(LBentry(prefix=prefix, name=name, isRoot=True))
                                    else:
                                        entryList.append(LBentry(prefix=prefix, name=name, arcrole=XbrlConst.parentChild, role=preferredLabel))
                            elif hasPresentationParentColumn:
                                preParent = cellValue(row, 'presentationParent', default='')
                                if preParent:
                                    (preParentPrefix, _sep, preParentName) = preParent.rpartition(':')
                                    preParentName = valueNameChars(preParentName)
                                    entryList = lbDepthList(preLB, topDepth)
                                    if (entryList is not None):
                                        preRel = (preParentPrefix, preParentName, prefix, name, (currentELR or currentELRdefinition))
                                        if (preRel not in preRels):
                                            entryList.append(LBentry(prefix=preParentPrefix, name=preParentName, isRoot=True, childStruct=[LBentry(prefix=prefix, name=name, arcrole=XbrlConst.parentChild, preferredLabel=preferredLabel)]))
                                            preRels.add(preRel)
                                        else:
                                            pass
                        if (hasDefLB and (topDepth != 999999)):
                            entryList = lbDepthList(defLB, depth)
                            if (entryList is not None):
                                if (depth == topDepth):
                                    if isConcept:
                                        entryList.append(LBentry(prefix=prefix, name=name, isRoot=True))
                                elif ((not preferredLabel) or (not any((((lbEntry.prefix == prefix) and (lbEntry.name == name)) for lbEntry in entryList)))):
                                    eltAttrs = {}
                                    parentLBentry = lbDepthList(defLB, (depth - 1))[(- 1)]
                                    parentName = parentLBentry.name
                                    parentEltAttrs = {}
                                    for doc in genDocs.values():
                                        if (name in doc.extensionElements):
                                            eltAttrs = doc.extensionElements.get(name, NULLENTRY)[0]
                                        if (parentName in doc.extensionElements):
                                            parentEltAttrs = doc.extensionElements.get(parentName, NULLENTRY)[0]
                                    if (isUSGAAP and (parentEltAttrs.get('substitutionGroup') == 'xbrldt:dimensionItem') and (eltAttrs.get('type') != 'nonnum:domainItemType')):
                                        typedDomainRef = ('#' + eltAttrs.get('id', ''))
                                        parentEltAttrs['{http://xbrl.org/2005/xbrldt}typedDomainRef'] = typedDomainRef
                                    elif isConcept:
                                        role = None
                                        if (('dimensionDefault' in headerCols) and cellHasValue(row, 'dimensionDefault', (str, bool))):
                                            v = cellValue(row, 'dimensionDefault', strip=True)
                                            if v:
                                                role = '_dimensionDefault_'
                                        entryList.append(LBentry(prefix=prefix, name=name, arcrole='_dimensions_', role=role))
                        if hasCalLB:
                            calcParents = cellValue(row, 'calculationParent', default='').split()
                            calcWeights = str(cellValue(row, 'calculationWeight', default='')).split()
                            if (calcParents and calcWeights):
                                for (i, calcParent) in enumerate(calcParents):
                                    calcWeight = (calcWeights[i] if (i < len(calcWeights)) else calcWeights[(- 1)])
                                    (calcParentPrefix, _sep, calcParentName) = calcParent.rpartition(':')
                                    calcParentName = valueNameChars(calcParentName)
                                    entryList = lbDepthList(calLB, topDepth)
                                    if (entryList is not None):
                                        calRel = (calcParentPrefix, calcParentName, prefix, name)
                                        if (calRel not in calRels):
                                            entryList.append(LBentry(prefix=calcParentPrefix, name=calcParentName, isRoot=True, childStruct=[LBentry(prefix=prefix, name=name, arcrole=XbrlConst.summationItem, weight=calcWeight)]))
                                            calRels.add(calRel)
                                        else:
                                            pass
                    hasRelationshipToCol = any(((h[0] == 'relationship to') for h in headerCols if isinstance(h, tuple)))
                if (useLabels or hasRelationshipToCol):
                    (prefix, name) = rowPrefixNameValues(row)
                    if ((name is not None) and ((prefix in genDocs) or extensionPrefixForCoreLabels or hasRelationshipToCol)):
                        thisDoc = genDocs.get((extensionPrefixForCoreLabels or prefix))
                        preferredLabel = cellValue(row, 'preferredLabel')
                        for (colItem, iCol) in headerCols.items():
                            if isinstance(colItem, tuple):
                                colItemType = colItem[0]
                                role = colItem[1]
                                lang = part = colItem[2]
                                cell = row[iCol]
                                v = xlValue(cell)
                                if ((v is None) or (isinstance(v, str) and (not v))):
                                    values = ()
                                else:
                                    v = str(v)
                                    if (colItemType in ('label', 'reference', 'relationship to')):
                                        values = (v,)
                                    elif (colItemType in ('labels', 'references')):
                                        values = v.split('\n')
                                if (preferredLabel and ('indented' in colItem) and (not hasPreferredLabelTextColumn)):
                                    role = preferredLabel
                                for (i, value) in enumerate(values):
                                    if (colItemType == 'relationship to'):
                                        entryList = lbDepthList(genLB, topDepth)
                                        if (entryList is not None):
                                            toName = value
                                            if (':' in toName):
                                                (toPrefix, _sep, toName) = value.partition(':')
                                            else:
                                                toPrefix = prefix
                                            if hasRelationshipAttributeColumn:
                                                relAttrs = None
                                                for header in headerCols:
                                                    if (isinstance(header, str) and header.startswith('relationship attribute, ')):
                                                        attrValue = cellValue(row, header)
                                                        if (attrValue not in (None, '')):
                                                            if (relAttrs is None):
                                                                relAttrs = {}
                                                            relAttrs[header[24:]] = attrValue
                                            entryList.append(LBentry(prefix=prefix, name=name, isRoot=True, childStruct=[LBentry(prefix=toPrefix, name=toName, arcrole=role, relAttrs=relAttrs)]))
                                    elif (thisDoc is None):
                                        pass
                                    elif (colItemType in ('label', 'labels')):
                                        if isConcept:
                                            if (hasPreferredLabelTextColumn and (role == '/preferredLabel')):
                                                role = preferredLabel
                                        elif (role == XbrlConst.standardLabel):
                                            role = XbrlConst.genStandardLabel
                                        elif (role == XbrlConst.documentationLabel):
                                            role = XbrlConst.genDocumentationLabel
                                        else:
                                            continue
                                        thisDoc.extensionLabels[(prefix, name, lang, role)] = value.strip()
                                    elif (hasRefLB and (colItemType == 'reference')):
                                        if isConcept:
                                            thisDoc.extensionReferences[(prefix, name, role)].add((part, value.strip()))
                                    elif (hasRefLB and (colItemType == 'references')):
                                        if isConcept:
                                            if role.endswith('#'):
                                                _role = '{}{:05.0f}'.format(role, i)
                                            else:
                                                _role = role
                                            _value = value.strip().replace('\\n', '\n')
                                            if (part is None):
                                                (_part, _sep, _value) = _value.partition(' ')
                                            else:
                                                _part = part
                                            thisDoc.extensionReferences[(prefix, name, _role)].add((_part, _value))
                        if (isConcept and eltEnumRefsParts and (thisDoc is not None)):
                            for (i, _enumRefParts) in enumerate(eltEnumRefsParts):
                                for ((colItemType, role, part), value) in _enumRefParts:
                                    if (colItemType == 'reference'):
                                        _role = '{}#{:05.0f}'.format(role, (i + 1))
                                        thisDoc.extensionReferences[(prefix, name, _role)].add((part, value.strip()))
                        thisDoc = None
            except Exception as err:
                fatalLoadingErrors.append('Excel sheet: {excelSheet}, row: {excelRow}, error: {error}, Traceback: {traceback}'.format(error=err, excelSheet=importSheetName, excelRow=iRow, traceback=traceback.format_tb(sys.exc_info()[2])))
        if (not headerCols):
            if (not conceptsWs):
                fatalLoadingErrors.append('Neither control worksheet (XBRL DTS tab) nor standard columns found, no DTS imported.')
            elif (not currentELR):
                fatalLoadingErrors.append('Extended link role not found, no DTS imported.')
    if fatalLoadingErrors:
        raise Exception(',\n '.join(fatalLoadingErrors))
    if (isUSGAAP and hasDefLB):

        def fixUsggapTableDims(lvl1Struct, level=0):
            foundTable = False
            emptyLinks = []
            foundHeadingItems = []
            foundLineItems = []
            for lvl1Entry in lvl1Struct:
                for lvl2Entry in lvl1Entry.childStruct:
                    if any((lvl2Entry.name.endswith(suffix) for suffix in ('Table', '_table', 'Cube', '_cube'))):
                        for lvl3Entry in lvl2Entry.childStruct:
                            if any((lvl3Entry.name.endswith(suffix) for suffix in ('LineItems', '_line_items'))):
                                foundLineItems.append((lvl1Entry, lvl2Entry, lvl3Entry))
                                foundTable = True
                                break
                    else:
                        foundHeadingItems.append((lvl1Entry, lvl2Entry))
                if (not foundLineItems):
                    foundNestedTable = fixUsggapTableDims(lvl1Entry.childStruct, (level + 1))
                    if ((level == 0) and (not foundNestedTable)):
                        emptyLinks.append(lvl1Entry)
                    foundTable |= foundNestedTable
                    del foundHeadingItems[:]
            for (lvl1Entry, lvl2Entry, lvl3Entry) in foundLineItems:
                i1 = lvl1Entry.childStruct.index(lvl2Entry)
                lvl1Entry.childStruct.insert(i1, lvl3Entry)
                lvl3Entry.childStruct.insert(0, lvl2Entry)
                if any((lvl1Entry.name.endswith(suffix) for suffix in ('Abstract', '_abstract', 'Root', '_root', '_package', '_heading'))):
                    lvl1Entry.childStruct.remove(lvl2Entry)
                lvl2Entry.childStruct.remove(lvl3Entry)
            for (lvl1Entry, lvl2Entry) in foundHeadingItems:
                lvl1Entry.childStruct.remove(lvl2Entry)
            for emptyLink in emptyLinks:
                lvl1Struct.remove(emptyLink)
            return foundTable
        fixUsggapTableDims(defLB)
    modelDocuments = []
    modelXbrl.blockDpmDBrecursion = True

    def generateDoc(thisDoc, parentDoc, visitedDocNames):
        if (thisDoc.name in visitedDocNames):
            modelXbrl.error('loadFromExcel:circularDependency', 'Generation order dependency is circular: %(circularDependency)s', modelXbrl=modelXbrl, circularDependency=((','.join(visitedDocNames) + ', ') + thisDoc.name))
            return
        visitedDocNames.append(thisDoc.name)
        if (XbrlConst.xsd not in thisDoc.importXmlns.values()):
            eltName = 'schema xmlns="{}"'.format(XbrlConst.xsd)
        else:
            for (k, v) in thisDoc.importXmlns.items():
                if (v == XbrlConst.xsd):
                    eltName = '{}:schema'.format(k)
                    break
        doc = createModelDocument(modelXbrl, Type.SCHEMA, thisDoc.extensionSchemaFilename, isEntry=(parentDoc is None), documentEncoding='utf-8', base='', initialXml='\n        <{eltName}\n            targetNamespace="{targetNamespace}" \n            attributeFormDefault="unqualified" \n            elementFormDefault="qualified" \n            xmlns:xs="http://www.w3.org/2001/XMLSchema" \n            xmlns:{extensionPrefix}="{targetNamespace}"\n            {importXmlns} \n            xmlns:nonnum="http://www.xbrl.org/dtr/type/non-numeric" \n            xmlns:link="http://www.xbrl.org/2003/linkbase" \n            xmlns:xbrli="http://www.xbrl.org/2003/instance" \n            xmlns:xlink="http://www.w3.org/1999/xlink" \n            xmlns:xbrldt="http://xbrl.org/2005/xbrldt" \n            {schemaVersion}{xmlLang} />\n        '.format(eltName=eltName, targetNamespace=thisDoc.extensionSchemaNamespaceURI, extensionPrefix=thisDoc.extensionSchemaPrefix, importXmlns=''.join(('xmlns:{0}="{1}"\n'.format(prefix, namespaceURI) for (prefix, namespaceURI) in thisDoc.importXmlns.items())), schemaVersion=('version="{}" '.format(thisDoc.extensionSchemaVersion) if thisDoc.extensionSchemaVersion else ''), xmlLang=('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else '')), initialComment=thisDoc.initialComment)
        if (parentDoc is None):
            modelXbrl.modelDocument = doc
        thisDoc.generated = True
        doc.loadedFromExcel = True
        doc.inDTS = True
        doc.targetNamespace = thisDoc.extensionSchemaNamespaceURI
        schemaElt = doc.xmlRootElement
        annotationElt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'annotation')
        if thisDoc.schemaDocumentation:
            XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'documentation', text=thisDoc.schemaDocumentation)
        appinfoElt = XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'appinfo')
        appinfoElt = XmlUtil.descendant(schemaElt, XbrlConst.xsd, 'appinfo')
        for filename in thisDoc.includes:
            XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'include', attributes=(('schemaLocation', filename),))
        for (importPrefix, importAttributes) in sorted(thisDoc.imports.items(), key=(lambda item: item[1])):
            XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'import', attributes=importAttributes)
            if ((importPrefix in genDocs) and (not genDocs[importPrefix].generated)):
                generateDoc(genDocs[importPrefix], doc, visitedDocNames)
        if any(((roleURI in thisDoc.extensionRoleLabels) for roleURI in thisDoc.extensionRoles.keys())):
            for importAttributes in ((('namespace', XbrlConst.gen), ('schemaLocation', 'http://www.xbrl.org/2008/generic-link.xsd')), (('namespace', XbrlConst.genLabel), ('schemaLocation', 'http://www.xbrl.org/2008/generic-label.xsd'))):
                XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'import', attributes=importAttributes)
        _enumNum = [1]

        def addFacets(thisDoc, restrElt, facets):
            if facets:
                excludedEnumeration = facets.get('excludedEnumeration')
                if ((annotateEnumerationsDocumentation and (excludedEnumeration == 'X')) or (excludedEnumeration == 'D')):
                    for (facet, facetValue) in facets.items():
                        if (facet == 'enumeration'):
                            enumerationsDocumentation = []
                            for valLbl in facetValue.split('\n'):
                                (val, _sep, _label) = valLbl.partition('=')
                                val = val.strip()
                                if len(val):
                                    if (val == '(empty)'):
                                        val = ''
                                    _label = _label.strip()
                                    enumerationsDocumentation.append(('{}: {}'.format(val, _label) if _label else val))
                            XmlUtil.addChild(XmlUtil.addChild(restrElt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=' \n'.join(enumerationsDocumentation))
                for (facet, facetValue) in sorted(facets.items(), key=(lambda i: facetSortOrder.get(i[0], i[0]))):
                    if (facet == 'enumeration'):
                        if ((not annotateEnumerationsDocumentation) and (not excludedEnumeration)):
                            for valLbl in facetValue.split('\n'):
                                (val, _sep, _label) = valLbl.partition('=')
                                val = val.strip()
                                _label = _label.strip()
                                if len(val):
                                    if (val == '(empty)'):
                                        val = ''
                                    _attributes = {'value': val}
                                    if _label:
                                        _labelsByLang = None
                                        if (_label.startswith('{') and _label.endswith('}')):
                                            try:
                                                _labelsByLang = json.loads(_label)
                                            except json.decoder.JSONDecodeError:
                                                _labelsByLang = None
                                        _name = 'enum{}'.format(_enumNum[0])
                                        _attributes['id'] = ((thisDoc.extensionSchemaPrefix + '_') + _name)
                                        _enumNum[0] += 1
                                        if _labelsByLang:
                                            for (_lang, _langLabel) in _labelsByLang.items():
                                                thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, _lang, XbrlConst.genStandardLabel)] = _langLabel
                                        else:
                                            thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, defaultLabelLang, XbrlConst.genStandardLabel)] = _label
                                    enumElt = XmlUtil.addChild(restrElt, XbrlConst.xsd, facet, attributes=_attributes)
                                    if (thisDoc.hasEnumerationDocumentation and _label):
                                        if _labelsByLang:
                                            annotationElt = XmlUtil.addChild(enumElt, XbrlConst.xsd, 'annotation')
                                            for (_lang, _langLabel) in _labelsByLang.items():
                                                thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, _lang, XbrlConst.genStandardLabel)] = _langLabel
                                                XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'documentation', text=_langLabel, attributes={'{http://www.w3.org/XML/1998/namespace}lang': _lang})
                                        else:
                                            XmlUtil.addChild(XmlUtil.addChild(enumElt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=_label)
                    elif (facet != 'excludedEnumeration'):
                        XmlUtil.addChild(restrElt, XbrlConst.xsd, facet, attributes={'value': str(facetValue)})
        for (eltName, eltDef) in sorted(thisDoc.extensionElements.items(), key=(lambda item: item[0])):
            (eltAttrs, eltFacets) = eltDef
            if (eltFacets and ('type' in eltAttrs)):
                eltType = eltAttrs['type']
                del eltAttrs['type']
            if any(((':' in attrname) for attrname in eltAttrs.keys())):
                for (attrname, attrvalue) in eltAttrs.copy().items():
                    if ((not attrname.startswith('{')) and (':' in attrname)):
                        del eltAttrs[attrname]
                        eltAttrs[schemaElt.prefixedNameQname(attrname).clarkNotation] = attrvalue
            isConcept = (eltAttrs.get('substitutionGroup') in ('xbrli:item', 'xbrli:tuple', 'xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))
            elt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'element', attributes=eltAttrs)
            if annotateElementDocumentation:
                for labelRole in (XbrlConst.documentationLabel, XbrlConst.genDocumentationLabel):
                    labelKey = (thisDoc.extensionSchemaPrefix, eltAttrs['name'], defaultLabelLang, labelRole)
                    if (labelKey in thisDoc.extensionLabels):
                        XmlUtil.addChild(XmlUtil.addChild(elt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=thisDoc.extensionLabels[labelKey])
                        break
            if ((elt is not None) and eltFacets and isConcept):
                cmplxType = XmlUtil.addChild(elt, XbrlConst.xsd, 'complexType')
                cmplxCont = XmlUtil.addChild(cmplxType, XbrlConst.xsd, 'simpleContent')
                restrElt = XmlUtil.addChild(cmplxCont, XbrlConst.xsd, 'restriction', attributes={'base': eltType})
                addFacets(thisDoc, restrElt, eltFacets)
                del eltType
        for (roleURI, (roleDefinition, usedOnRoles)) in sorted(thisDoc.extensionRoles.items(), key=(lambda rd: rd[1])):
            roleElt = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'roleType', attributes=(('roleURI', roleURI), ('id', ('roleType_' + roleURI.rpartition('/')[2]))))
            if roleDefinition:
                XmlUtil.addChild(roleElt, XbrlConst.link, 'definition', text=roleDefinition)
            if usedOnRoles:
                for usedOnRole in usedOnRoles.split():
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text=usedOnRole)
            else:
                if (hasPreLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in preLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:presentationLink')
                if (hasDefLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in defLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:definitionLink')
                if (hasCalLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in calLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:calculationLink')
                if (hasGenLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in genLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text=qname('{http://xbrl.org/2008/generic}genlink:link'))
        if any(((roleURI in thisDoc.extensionRoleLabels) for roleURI in thisDoc.extensionRoles.keys())):
            genLabLB = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'linkbase')
            XmlUtil.addChild(genLabLB, XbrlConst.link, 'roleRef', attributes=(('roleURI', XbrlConst.genStandardLabel), ('{http://www.w3.org/1999/xlink}href', 'http://www.xbrl.org/2008/generic-label.xsd#standard-label'), ('{http://www.w3.org/1999/xlink}type', 'simple')))
            XmlUtil.addChild(genLabLB, XbrlConst.link, 'arcroleRef', attributes=(('arcroleURI', elementLabel), ('{http://www.w3.org/1999/xlink}href', 'http://www.xbrl.org/2008/generic-label.xsd#element-label'), ('{http://www.w3.org/1999/xlink}type', 'simple')))
            linkElt = XmlUtil.addChild(genLabLB, qname('{http://xbrl.org/2008/generic}genlink:link'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            for (roleURI, _defLabel) in sorted(thisDoc.extensionRoles.items(), key=(lambda rd: rd[0])):
                if (roleURI in thisDoc.extensionRoleLabels):
                    xlLabel = roleURI.rpartition('/')[2]
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', ('#roleType_' + xlLabel)), ('{http://www.w3.org/1999/xlink}label', ('loc_' + xlLabel))))
                    XmlUtil.addChild(linkElt, XbrlConst.qnGenArc, attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', elementLabel), ('{http://www.w3.org/1999/xlink}from', ('loc_' + xlLabel)), ('{http://www.w3.org/1999/xlink}to', ('label_' + xlLabel))))
                    for (text, lang) in thisDoc.extensionRoleLabels[roleURI]:
                        XmlUtil.addChild(linkElt, qname('{http://xbrl.org/2008/label}genlabel:label'), attributes=(('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + xlLabel)), ('{http://www.w3.org/1999/xlink}role', XbrlConst.genStandardLabel), ('{http://www.w3.org/XML/1998/namespace}lang', lang)), text=text)

        def addLinkbaseRef(lbType, lbFilename, lbDoc):
            role = 'http://www.xbrl.org/2003/role/{0}LinkbaseRef'.format(lbType)
            lbRefElt = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'linkbaseRef', attributes=((('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', docRelpath(lbFilename, thisDoc.extensionSchemaRelDirname)), ('{http://www.w3.org/1999/xlink}arcrole', 'http://www.w3.org/1999/xlink/properties/linkbase')) + (() if lbType.startswith('generic') else (('{http://www.w3.org/1999/xlink}role', role),))))
            if lbDoc:
                doc.referencesDocument[lbDoc] = ModelDocumentReference('href', lbRefElt)
        for (lbRefType, filename, generate) in thisDoc.linkbaseRefs:
            if (not generate):
                for otherGenDoc in genDocs.values():
                    if ((not otherGenDoc.generated) and any((((_otherLbRefType == lbRefType) and (_otherFilename == filename) and _otherGenerate) for (_otherLbRefType, _otherFilename, _otherGenerate) in otherGenDoc.linkbaseRefs))):
                        generateDoc(otherGenDoc, doc, visitedDocNames)
                addLinkbaseRef(lbRefType, filename, None)
        doc.schemaDiscover(schemaElt, False, thisDoc.extensionSchemaNamespaceURI)
        for (typeName, typeDef) in sorted(thisDoc.extensionTypes.items(), key=(lambda item: item[0])):
            if (qname(thisDoc.extensionSchemaNamespaceURI, typeName) in modelXbrl.qnameTypes):
                continue
            (typeAttrs, typeFacets) = typeDef
            if (typeName.endswith('ItemType') or typeAttrs.get('base', '').endswith('ItemType')):
                cmplxType = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'complexType', attributes={'name': typeAttrs['name']})
                contElt = XmlUtil.addChild(cmplxType, XbrlConst.xsd, 'simpleContent')
            else:
                contElt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'simpleType', attributes={'name': typeAttrs['name']})
            restrElt = XmlUtil.addChild(contElt, XbrlConst.xsd, 'restriction', attributes={'base': typeAttrs['base']})
            baseQn = qname(schemaElt, typeAttrs.get('base'))
            if typeFacets:
                if (baseQn and (baseQn.namespaceURI not in (XbrlConst.xsd, XbrlConst.xbrli)) and (baseQn in modelXbrl.qnameTypes)):
                    baseTypeFacets = (modelXbrl.qnameTypes[baseQn].facets or ())
                    typeFacets = dict(((facet, value) for (facet, value) in typeFacets.items() if ((facet not in baseTypeFacets) or (str(baseTypeFacets[facet]) != value))))
                addFacets(thisDoc, restrElt, typeFacets)
        extLabelRoles = {}
        extReferenceRoles = {}
        extReferenceParts = {}
        extReferenceSchemaDocs = {}
        extUnrecognizedRoles = set()
        relationshipArcroles = {}
        relationshipArcqnames = {}

        def setExtRefPart(partLocalName):
            if (partLocalName not in extReferenceParts):
                for partConcept in modelXbrl.nameConcepts.get(partLocalName, ()):
                    if ((partConcept is not None) and (partConcept.subGroupHeadQname == qnLinkPart)):
                        extReferenceParts[partLocalName] = partConcept.qname
                        extReferenceSchemaDocs[partConcept.qname.namespaceURI] = (partConcept.modelDocument.uri if partConcept.modelDocument.uri.startswith('http://') else partConcept.modelDocument.basename)
                        break
        for _headerColKey in headerColsAllElrs:
            if (isinstance(_headerColKey, tuple) and (len(_headerColKey) >= 3) and (not _headerColKey[1].startswith('http://'))):
                _resourceType = _headerColKey[0]
                _resourceRole = _headerColKey[1]
                _resourceLangOrPart = _headerColKey[2]
            elif (isinstance(_headerColKey, str) and ('!reference' in _headerColKey)):
                m = resourceParsePattern.match(_headerColKey.partition('!')[2])
                _resourceType = m.group(1)
                _resourceRole = ('/' + m.group(2))
                _resourceLangOrPart = m.group(4)
            else:
                continue
            (_resourceQName, _standardRoles) = {'label': (qnLinkLabel, standardLabelRoles), 'labels': (qnLinkLabel, standardLabelRoles), 'reference': (qnLinkReference, standardReferenceRoles), 'references': (qnLinkReference, standardReferenceRoles)}.get(_resourceType, (None, ()))
            _resourceRoleURI = None
            for _roleURI in _standardRoles:
                if _roleURI.endswith(_resourceRole):
                    _resourceRoleURI = _roleURI
                    _resourceRoleMatchPart = _resourceRole
                    break
            if (_resourceRoleURI is None):
                _resourceRoleMatchPart = _resourceRole.partition('#')[0]
                for _roleURI in modelXbrl.roleTypes:
                    if _roleURI.endswith(_resourceRoleMatchPart):
                        for _roleType in modelXbrl.roleTypes[_roleURI]:
                            if (_resourceQName in _roleType.usedOns):
                                _resourceRoleURI = _roleURI
                                break
            if (_resourceType in ('label', 'labels')):
                if _resourceRoleURI:
                    extLabelRoles[_resourceRoleMatchPart] = _resourceRoleURI
                elif any(((_resourceRoleMatchPart == k[2]) for k in thisDoc.extensionLabels.keys())):
                    modelXbrl.error('loadFromExcel:labelResourceRole', 'Label resource role not found: %(role)s', modelXbrl=modelXbrl, role=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
            elif (_resourceType in ('reference', 'references')):
                if _resourceRoleURI:
                    extReferenceRoles[_resourceRoleMatchPart] = _resourceRoleURI
                    setExtRefPart(_resourceLangOrPart)
                elif any(((_resourceRoleMatchPart == k[2]) for k in thisDoc.extensionReferences.keys())):
                    modelXbrl.error('loadFromExcel:referenceResourceRole', 'Reference resource role not found: %(role)s', modelXbrl=modelXbrl, role=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
            elif (_resourceType == 'relationship to'):
                for _arcroleURI in modelXbrl.arcroleTypes:
                    if _arcroleURI.endswith(_resourceRoleMatchPart):
                        for _arcroleType in modelXbrl.arcroleTypes[_arcroleURI]:
                            for _resourceQName in _arcroleType.usedOns:
                                break
                        break
                if (_resourceQName is None):
                    modelXbrl.error('loadFromExcel:relationshipArcrole', 'Relationship arcrole not found: %(arcrole)s', modelXbrl=modelXbrl, arcrole=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
                else:
                    relationshipArcroles[_resourceRoleMatchPart] = _arcroleURI
                    relationshipArcqnames[_arcroleURI] = _resourceQName
        for (lbType, lang, filename) in thisDoc.labelLinkbases:
            thisDoc.thisLBdir = posixpath.dirname(filename)
            langPattern = re.compile((lang or '.*'))
            _isGeneric = lbType.startswith('generic')
            if (_isGeneric and ('http://xbrl.org/2008/label' not in modelXbrl.namespaceDocs)):
                ModelDocument.load(modelXbrl, 'http://www.xbrl.org/2008/generic-link.xsd', isDiscovered=True)
                ModelDocument.load(modelXbrl, 'http://www.xbrl.org/2008/generic-label.xsd', isDiscovered=True)
            lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base='', initialXml='\n            <linkbase \n                xmlns="http://www.xbrl.org/2003/linkbase" \n                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                xmlns:xlink="http://www.w3.org/1999/xlink" \n                xmlns:xbrli="http://www.xbrl.org/2003/instance"\n                {}\n                xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}" \n                {}>{}</linkbase>\n            '.format(('\n                xmlns:genlink="http://xbrl.org/2008/generic"\n                xmlns:genlabel="http://xbrl.org/2008/label"\n                ' if _isGeneric else ''), ('\n                http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                http://xbrl.org/2008/label  http://www.xbrl.org/2008/generic-label.xsd\n                ' if _isGeneric else ''), ('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else ''), ('\n                <arcroleRef arcroleURI="http://xbrl.org/arcrole/2008/element-label" xlink:href="http://www.xbrl.org/2008/generic-label.xsd#element-label" xlink:type="simple"/>\n                ' if _isGeneric else '')), initialComment=thisDoc.initialComment)
            lbDoc.inDTS = True
            lbDoc.loadedFromExcel = True
            if isGenerateAndImport:
                addLinkbaseRef(lbType, filename, lbDoc)
            lbElt = lbDoc.xmlRootElement
            linkElt = XmlUtil.addChild(lbElt, (gen if _isGeneric else link), ('link' if _isGeneric else 'labelLink'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            firstLinkElt = linkElt
            locs = set()
            roleRefs = set()
            for (labelKey, text) in thisDoc.extensionLabels.items():
                (prefix, name, labelLang, role) = labelKey
                labelLang = (labelLang or defaultLabelLang)
                role = role.partition('#')[0]
                role = extLabelRoles.get(role, role)
                if (langPattern.match(labelLang) and (_isGeneric == (role in (XbrlConst.genStandardLabel, XbrlConst.genDocumentationLabel)))):
                    locLabel = ((prefix + '_') + name)
                    if (locLabel not in locs):
                        locs.add(locLabel)
                        XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', LBHref(thisDoc, prefix, name)), ('{http://www.w3.org/1999/xlink}label', locLabel)))
                        XmlUtil.addChild(linkElt, (gen if _isGeneric else link), ('arc' if _isGeneric else 'labelArc'), attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', (elementLabel if _isGeneric else conceptLabel)), ('{http://www.w3.org/1999/xlink}from', locLabel), ('{http://www.w3.org/1999/xlink}to', ('label_' + locLabel)), ('order', 1.0)))
                    XmlUtil.addChild(linkElt, (XbrlConst.genLabel if _isGeneric else XbrlConst.link), 'label', attributes=((('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + locLabel)), ('{http://www.w3.org/1999/xlink}role', role)) + ((('{http://www.w3.org/XML/1998/namespace}lang', labelLang),) if (True or (lang != saveXmlLang)) else ())), text=text)
                    if role:
                        if (role in XbrlConst.standardLabelRoles):
                            pass
                        elif (role in modelXbrl.roleTypes):
                            roleType = modelXbrl.roleTypes[role][0]
                            roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                        elif role.startswith('http://www.xbrl.org/2009/role/negated'):
                            roleRefs.add(('roleRef', role, ('http://www.xbrl.org/lrr/role/negated-2009-12-16.xsd#' + role.rpartition('/')[2])))
                        else:
                            extUnrecognizedRoles.add(role)
            for (roleref, roleURI, href) in roleRefs:
                XmlUtil.addChild(lbElt, XbrlConst.link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', href)), beforeSibling=firstLinkElt)
            lbDoc.linkbaseDiscover(lbElt)
            if extUnrecognizedRoles:
                modelXbrl.error('loadFromExcel:undefinedLabelRole', 'Label roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                extUnrecognizedRoles.clear()
        for (lbType, referenceRole, filename) in thisDoc.referenceLinkbases:
            thisDoc.thisLBdir = posixpath.dirname(filename)
            _isGeneric = lbType.startswith('generic')
            lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base='', initialXml='\n            <linkbase \n                xmlns="http://www.xbrl.org/2003/linkbase" \n                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                xmlns:xlink="http://www.w3.org/1999/xlink" \n                xmlns:xbrli="http://www.xbrl.org/2003/instance"\n                {}\n                xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}{}" \n                {}>{}</linkbase>\n            '.format(('\n                xmlns:genlink="http://xbrl.org/2008/generic"\n                xmlns:genreference="http://xbrl.org/2008/rerference"\n                ' if _isGeneric else ''), ''.join([' {} {}'.format(_ns, _uri) for (_ns, _uri) in extReferenceSchemaDocs.items()]), ('\n                http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                http://xbrl.org/2008/reference  http://www.xbrl.org/2008/generic-reference.xsd\n                ' if _isGeneric else ''), ('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else ''), ('\n                <roleRef roleURI="http://www.xbrl.org/2008/role/label" xlink:href="http://www.xbrl.org/2008/generic-label.xsd#standard-label" xlink:type="simple"/>\n                <arcroleRef arcroleURI="http://xbrl.org/arcrole/2008/element-reference" xlink:href="http://xbrl.org/2008/generic-reference.xsd#element-reference" xlink:type="simple"/>\n                ' if _isGeneric else '')), initialComment=thisDoc.initialComment)
            lbDoc.inDTS = True
            lbDoc.loadedFromExcel = True
            if isGenerateAndImport:
                addLinkbaseRef(lbType, filename, lbDoc)
            lbElt = lbDoc.xmlRootElement
            linkElt = XmlUtil.addChild(lbElt, (XbrlConst.gen if _isGeneric else XbrlConst.link), ('link' if _isGeneric else 'referenceLink'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            firstLinkElt = linkElt
            locs = set()
            roleRefs = set()
            undefinedReferenceParts = set()
            for (referenceKey, references) in thisDoc.extensionReferences.items():
                (prefix, name, role) = referenceKey
                role = role.partition('#')[0]
                role = extReferenceRoles.get(role, role)
                if fnmatch(role, referenceRole):
                    locLabel = ((prefix + '_') + name)
                    if (locLabel not in locs):
                        locs.add(locLabel)
                        order = 1
                    else:
                        for order in range(2, 1000):
                            _locLabel = '{}_{}'.format(locLabel, order)
                            if (_locLabel not in locs):
                                locLabel = _locLabel
                                locs.add(locLabel)
                                break
                        if (order > 999):
                            print('resource order de-duplicate failure, too many reference parts')
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', LBHref(thisDoc, prefix, name)), ('{http://www.w3.org/1999/xlink}label', locLabel)))
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'referenceArc', attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', conceptReference), ('{http://www.w3.org/1999/xlink}from', locLabel), ('{http://www.w3.org/1999/xlink}to', ('label_' + locLabel)), ('order', order)))
                    referenceResource = XmlUtil.addChild(linkElt, (XbrlConst.genReference if _isGeneric else XbrlConst.link), 'reference', attributes=(('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + locLabel)), ('{http://www.w3.org/1999/xlink}role', role)))
                    for (part, text) in references:
                        setExtRefPart(part)
                        if (part in extReferenceParts):
                            partQn = extReferenceParts.get(part, part)
                            XmlUtil.addChild(referenceResource, partQn, text=text)
                        else:
                            undefinedReferenceParts.add(part)
                    if role:
                        if (role in XbrlConst.standardLabelRoles):
                            pass
                        elif (role in modelXbrl.roleTypes):
                            roleType = modelXbrl.roleTypes[role][0]
                            roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                        elif role.startswith('http://www.xbrl.org/2009/role/negated'):
                            roleRefs.add(('roleRef', role, ('http://www.xbrl.org/lrr/role/negated-2009-12-16.xsd#' + role.rpartition('/')[2])))
                        else:
                            extUnrecognizedRoles.add(role)
            for part in sorted(undefinedReferenceParts):
                print('reference part not defined: {}'.format(part))
            for (roleref, roleURI, href) in roleRefs:
                XmlUtil.addChild(lbElt, XbrlConst.link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', href)), beforeSibling=firstLinkElt)
            lbDoc.linkbaseDiscover(lbElt)
            if extUnrecognizedRoles:
                modelXbrl.error('loadFromExcel:undefinedReferenceRole', 'Reference roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                extUnrecognizedRoles.clear()
        prefixedNamespaces = modelXbrl.prefixedNamespaces

        def hrefConcept(prefix, name):
            qn = qname(prefixedNamespaces[prefix], name)
            if (qn in modelXbrl.qnameConcepts):
                return modelXbrl.qnameConcepts[qn]
            elif (name in modelXbrl.nameConcepts):
                return modelXbrl.nameConcepts[name][0]
            if (prefix not in prefixedNamespaces):
                modelXbrl.error('loadFromExcel:undefinedRelationshipElementPrefix', 'Prefix not defined: %(prefix)s', modelXbrl=modelXbrl, prefix=prefix)
                return None
            modelXbrl.error('loadFromExcel:undefinedRelationshipElement', 'QName not defined: %(prefix)s:%(localName)s', modelXbrl=modelXbrl, prefix=prefix, localName=name)
            return None

        def prefixedNameQName(prefixedName):
            if (':' not in prefixedName):
                return prefixedName
            (prefix, _sep, name) = prefixedName.rpartition(':')
            if (prefix not in prefixedNamespaces):
                modelXbrl.error('loadFromExcel:undefinedRelationshipAttributePrefix', 'Prefix not defined: %(prefix)s', modelXbrl=modelXbrl, prefix=prefix)
                return prefixedName
            return QName(prefix, prefixedNamespaces[prefix], name)

        def lbTreeWalk(lbType, parentElt, lbStruct, roleRefs, dimDef=False, locs=None, arcsFromTo=None, fromPrefix=None, fromName=None):
            order = 1.0
            for lbEntry in lbStruct:
                if lbEntry.isELR:
                    if (not lbEntry.childStruct):
                        continue
                    role = 'unspecified'
                    if (lbEntry.role and lbEntry.role.startswith('http://')):
                        role = lbEntry.role
                    elif lbEntry.name:
                        for (linkroleUri, modelRoleTypes) in modelXbrl.roleTypes.items():
                            definition = modelRoleTypes[0].definition
                            if ((lbEntry.name == definition) and (linkroleUri in thisDoc.extensionRoles)):
                                role = linkroleUri
                                break
                    if (role == 'unspecified'):
                        continue
                    if (role not in thisDoc.extensionRoles):
                        continue
                    if (role == XbrlConst.defaultLinkRole):
                        pass
                    elif (role in thisDoc.extensionRoles):
                        roleRefs.add(('roleRef', role, ((doc.uri + '#roleType_') + role.rpartition('/')[2])))
                    elif (role in modelXbrl.roleTypes):
                        roleType = modelRoleTypes[0]
                        roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                    else:
                        extUnrecognizedRoles.add(role)
                    linkElt = XmlUtil.addChild(parentElt, (XbrlConst.gen if (lbType == 'generic') else XbrlConst.link), ('link' if (lbType == 'generic') else (lbType + 'Link')), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', role)))
                    locs = set()
                    arcsFromTo = set()
                    lbTreeWalk(lbType, linkElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo)
                else:
                    toPrefix = lbEntry.prefix
                    toName = lbEntry.name
                    toHref = LBHref(thisDoc, toPrefix, toName)
                    if (toHref is None):
                        modelXbrl.error('loadFromExcel:invalidQName', "%(linkbase)s relationship element with prefix '%(prefix)s' localName '%(localName)s' not found", modelXbrl=modelXbrl, linkbase=lbType, prefix=lbEntry.prefix, localName=lbEntry.name)
                        continue
                    if ((not toPrefix) and (toName in modelXbrl.nameConcepts)):
                        toPrefix = modelXbrl.nameConcepts[toName][0].qname.prefix
                    toLabel = '{}_{}'.format(toPrefix, toName)
                    toLabelAlt = None
                    if (not lbEntry.isRoot):
                        if ((not fromPrefix) and (fromName in modelXbrl.nameConcepts)):
                            fromPrefix = modelXbrl.nameConcepts[fromName][0].qname.prefix
                        fromLabel = '{}_{}'.format(fromPrefix, fromName)
                        if ((fromLabel, toLabel) in arcsFromTo):
                            for i in range(1, 1000):
                                toLabelAlt = '{}_{}'.format(toLabel, i)
                                if ((fromLabel, toLabelAlt) not in arcsFromTo):
                                    toLabel = toLabelAlt
                                    break
                    if (((toHref not in locs) or toLabelAlt) and (not dimDef)):
                        XmlUtil.addChild(parentElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', toHref), ('{http://www.w3.org/1999/xlink}label', toLabel)))
                        locs.add(toHref)
                    if (not lbEntry.isRoot):
                        arcsFromTo.add((fromLabel, toLabel))
                        if ((lbType == 'calculation') and (lbEntry.weight is not None)):
                            otherAttrs = (('weight', lbEntry.weight),)
                        elif ((lbType == 'presentation') and lbEntry.role):
                            if (not lbEntry.role.startswith('http://')):
                                _labelRoleMatchPart = ('/' + lbEntry.role)
                                for _roleURI in modelXbrl.roleTypes:
                                    if _roleURI.endswith(_labelRoleMatchPart):
                                        for _roleType in modelXbrl.roleTypes[_roleURI]:
                                            if (XbrlConst.qnLinkLabel in _roleType.usedOns):
                                                lbEntry.role = _roleURI
                                                break
                            if (not lbEntry.role.startswith('http://')):
                                lbEntry.role = ('http://www.xbrl.org/2003/role/' + lbEntry.role)
                            otherAttrs = (('preferredLabel', lbEntry.role),)
                            if (lbEntry.role and (lbEntry.role not in XbrlConst.standardLabelRoles)):
                                if (lbEntry.role in modelXbrl.roleTypes):
                                    roleType = modelXbrl.roleTypes[lbEntry.role][0]
                                    roleRefs.add(('roleRef', lbEntry.role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                                else:
                                    extUnrecognizedRoles.add(lbEntry.role)
                        elif ((lbType == 'generic') and lbEntry.arcrole):
                            if (not lbEntry.arcrole.startswith('http://')):
                                for _arcroleURI in modelXbrl.arcroleTypes:
                                    if _arcroleURI.endswith(lbEntry.arcrole):
                                        lbEntry.arcrole = _arcroleURI
                                        break
                            otherAttrs = tuple(((prefixedNameQName(_key), _value) for (_key, _value) in (lbEntry.relAttrs.items() if (lbEntry.relAttrs is not None) else ())))
                        else:
                            otherAttrs = ()
                        if (lbEntry.arcrole == '_dimensions_'):
                            fromConcept = hrefConcept(fromPrefix, fromName)
                            toConcept = hrefConcept(toPrefix, toName)
                            if dimDef:
                                if ((lbEntry.role != '_dimensionDefault_') and (not lbTreeHasDimDefault(lbEntry.childStruct))):
                                    continue
                                if ((toConcept is not None) and (toConcept.isDimensionItem or (lbEntry.role == '_dimensionDefault_'))):
                                    if ((toHref not in locs) or toLabelAlt):
                                        XmlUtil.addChild(parentElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', toHref), ('{http://www.w3.org/1999/xlink}label', toLabel)))
                                        locs.add(toHref)
                                    if (lbEntry.role != '_dimensionDefault_'):
                                        lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, toPrefix, toName)
                                    else:
                                        XmlUtil.addChild(parentElt, XbrlConst.link, 'definitionArc', attributes=((('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', XbrlConst.dimensionDefault), ('{http://www.w3.org/1999/xlink}from', fromLabel), ('{http://www.w3.org/1999/xlink}to', toLabel), ('order', order)) + otherAttrs))
                                        order += 1.0
                                else:
                                    lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, fromPrefix, fromName)
                                continue
                            elif ((toConcept is not None) and toConcept.isHypercubeItem):
                                arcrole = XbrlConst.all
                                otherAttrs += ((XbrlConst.qnXbrldtContextElement, 'segment'), (qnXbrldtClosed, 'true'))
                            elif ((toConcept is not None) and toConcept.isDimensionItem):
                                arcrole = XbrlConst.hypercubeDimension
                            elif ((fromConcept is not None) and fromConcept.isDimensionItem):
                                arcrole = XbrlConst.dimensionDomain
                            else:
                                arcrole = XbrlConst.domainMember
                        else:
                            arcrole = lbEntry.arcrole
                        if (arcrole in relationshipArcqnames):
                            arcqname = relationshipArcqnames[arcrole]
                            arcNS = arcqname.namespaceURI
                            arcLocalname = arcqname.localName
                        elif (lbType == 'generic'):
                            arcNS = XbrlConst.gen
                            arcLocalname = 'arc'
                        else:
                            arcNS = XbrlConst.link
                            arcLocalname = (lbType + 'Arc')
                        XmlUtil.addChild(parentElt, arcNS, arcLocalname, attributes=((('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', arcrole), ('{http://www.w3.org/1999/xlink}from', fromLabel), ('{http://www.w3.org/1999/xlink}to', toLabel), ('order', order)) + otherAttrs))
                        order += 1.0
                    if ((lbType != 'calculation') or lbEntry.isRoot):
                        lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, toPrefix, toName)

        def lbTreeHasDimDefault(lbStruct):
            for lbEntry in lbStruct:
                if lbEntry.isELR:
                    if (not lbEntry.childStruct):
                        continue
                    if lbTreeHasDimDefault(lbEntry.childStruct):
                        return True
                else:
                    if ((not lbEntry.isRoot) and ((lbEntry.arcrole == '_dimensions_') and (lbEntry.role == '_dimensionDefault_'))):
                        return True
                    if lbTreeHasDimDefault(lbEntry.childStruct):
                        return True
            return False
        for (hasLB, lbType, lbLB) in (((hasPreLB and thisDoc.hasPreLB), 'presentation', preLB), ((hasDefLB and thisDoc.hasDefLB), 'definition', defLB), ((hasCalLB and thisDoc.hasCalLB), 'calculation', calLB), ((hasGenLB and thisDoc.hasGenLB), 'generic', genLB)):
            if hasLB:
                for (lbRefType, filename, generate) in thisDoc.linkbaseRefs:
                    thisDoc.thisLBdir = posixpath.dirname(filename)
                    if (generate and (lbType == lbRefType)):
                        lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base='', initialXml='\n                        <linkbase \n                            xmlns="http://www.xbrl.org/2003/linkbase" \n                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                            xmlns:xlink="http://www.w3.org/1999/xlink" \n                            xmlns:xbrli="http://www.xbrl.org/2003/instance"{}\n                            xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                            http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}" \n                            />\n                        '.format(('\n                        xmlns:generic="http://xbrl.org/2008/generic"\n                        ' if (lbType == 'generic') else ''), ('\n                        http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                        ' if (lbType == 'generic') else '')), initialComment=thisDoc.initialComment)
                        lbDoc.inDTS = True
                        lbDoc.loadedFromExcel = True
                        addLinkbaseRef(lbRefType, filename, lbDoc)
                        lbElt = lbDoc.xmlRootElement
                        roleRefs = set()
                        if (lbType == 'definition'):
                            roleRefs.update((('arcroleRef', XbrlConst.all, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#all'), ('arcroleRef', XbrlConst.dimensionDefault, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#dimension-default'), ('arcroleRef', XbrlConst.dimensionDomain, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#dimension-domain'), ('arcroleRef', XbrlConst.domainMember, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#domain-member'), ('arcroleRef', XbrlConst.hypercubeDimension, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#hypercube-dimension')))
                        elif (lbType == 'generic'):
                            for _arcroleURI in relationshipArcroles.values():
                                for _arcroleType in modelXbrl.arcroleTypes[_arcroleURI]:
                                    roleRefs.add(('arcroleRef', _arcroleURI, ((_arcroleType.modelDocument.uri + '#') + _arcroleType.id)))
                                    break
                        lbTreeWalk(lbType, lbElt, lbLB, roleRefs)
                        if ((lbType == 'definition') and lbTreeHasDimDefault(lbLB)):
                            lbTreeWalk(lbType, lbElt, lbLB, roleRefs, dimDef=True)
                        firstLinkElt = None
                        for firstLinkElt in lbElt.iterchildren():
                            break
                        for (roleref, roleURI, href) in roleRefs:
                            XmlUtil.addChild(lbElt, link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', docRelpath(href, thisDoc.thisLBdir))), beforeSibling=firstLinkElt)
                        lbDoc.linkbaseDiscover(lbElt)
                        break
                if extUnrecognizedRoles:
                    modelXbrl.error('loadFromExcel:undefinedRole', '%(lbType)s linkbase roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, lbType=lbType, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                    extUnrecognizedRoles.clear()
        visitedDocNames.pop()

    def LBHref(thisDoc, prefix, name):
        if ((not prefix) and (name in modelXbrl.nameConcepts)):
            _concept = modelXbrl.nameConcepts[name][0]
            filename = _concept.modelDocument.uri
            prefix = _concept.qname.prefix
        elif (prefix == thisDoc.extensionSchemaPrefix):
            filename = thisDoc.extensionSchemaFilename
        elif (prefix in thisDoc.importFilenames):
            filename = thisDoc.importFilenames[prefix]
        elif (prefix in genDocs):
            doc = genDocs[prefix]
            if (not doc.generated):
                generateDoc(doc, thisDoc)
            if doc.generated:
                filename = doc.extensionSchemaFilename
            else:
                return None
        elif (name in modelXbrl.nameConcepts):
            filename = None
            for _concept in modelXbrl.nameConcepts[name]:
                if (prefix == _concept.qname.prefix):
                    filename = _concept.modelDocument.uri
                    break
            if (not filename):
                return None
        else:
            return None
        return '{0}#{1}_{2}'.format(docRelpath(filename, thisDoc.thisLBdir), prefix, name)
    for thisDoc in genOrder:
        if (not thisDoc.generated):
            generateDoc(thisDoc, None, [])
    if priorCWD:
        os.chdir(priorCWD)
    return modelXbrl.modelDocument
def loadFromExcel(cntlr, modelXbrl, excelFile, mappedUri):
    from openpyxl import load_workbook
    from arelle import ModelDocument, ModelXbrl, XmlUtil
    from arelle.ModelDocument import ModelDocumentReference
    from arelle.ModelValue import qname

    def xlUnicodeChar(match):
        return chr(int(match.group(1), 16))

    def xlValue(cell):
        v = cell.value
        if isinstance(v, str):
            return xlUnicodePattern.sub(xlUnicodeChar, v).replace('\r\n', '\n').replace('\r', '\n')
        return v
    defaultLabelLang = (saveXmlLang or 'en')
    importColumnHeaders = {'名前空間プレフィックス': 'prefix', 'prefix': 'prefix', '要素名': 'name', 'name': 'name', 'type': 'type', 'typePrefix': 'typePrefix', 'substitutionGroup': 'substitutionGroup', 'periodType': 'periodType', 'balance': 'balance', 'abstract': 'abstract', 'nillable': 'nillable', 'depth': 'depth', 'minLength': 'minLength', 'maxLength': 'maxLength', 'minInclusive': 'minInclusive', 'maxInclusive': 'maxInclusive', 'length': 'length', 'fixed': 'fixed', 'pattern': 'pattern', 'enumeration': 'enumeration', 'excludedEnumeration': 'excludedEnumeration', 'preferred label': 'preferredLabel', 'preferredLabel': 'preferredLabel', 'presentation parent': 'presentationParent', 'calculation parent': 'calculationParent', 'calculation weight': 'calculationWeight', '標準ラベル（日本語）': ('label', XbrlConst.standardLabel, 'ja', 'indented'), '冗長ラベル（日本語）': ('label', XbrlConst.verboseLabel, 'ja'), '標準ラベル（英語）': ('label', XbrlConst.standardLabel, 'en'), '冗長ラベル（英語）': ('label', XbrlConst.verboseLabel, 'en'), '用途区分、財務諸表区分及び業種区分のラベル（日本語）': ('labels', XbrlConst.standardLabel, 'ja'), '用途区分、財務諸表区分及び業種区分のラベル（英語）': ('labels', XbrlConst.standardLabel, 'en'), 'label': ('label', XbrlConst.standardLabel, defaultLabelLang, 'indented'), 'label, standard': ('label', XbrlConst.standardLabel, defaultLabelLang, 'overridePreferred'), 'label, terse': ('label', XbrlConst.terseLabel, defaultLabelLang), 'label, verbose': ('label', XbrlConst.verboseLabel, defaultLabelLang), 'label, documentation': ('label', XbrlConst.documentationLabel, defaultLabelLang), 'group': 'linkrole', 'linkrole': 'linkrole', 'ELR': 'linkrole', 'dimension default': 'dimensionDefault'}
    fatalLoadingErrors = []
    startedAt = time.time()
    if os.path.isabs(excelFile):
        priorCWD = os.getcwd()
        os.chdir(os.path.dirname(excelFile))
    else:
        priorCWD = None
    importExcelBook = load_workbook(excelFile, data_only=True)
    sheetNames = importExcelBook.get_sheet_names()
    dtsSheet = None
    if ('XBRL DTS' in sheetNames):
        dtsSheet = 'XBRL DTS'
    elif ('DTS' in sheetNames):
        dtsSheet = 'DTS'
    elif ('Sheet2' in sheetNames):
        dtsSheet = 'Sheet2'
    if dtsSheet:
        dtsWs = importExcelBook[dtsSheet]
    else:
        dtsWs = None
    imports = {'xbrli': (('namespace', XbrlConst.xbrli), ('schemaLocation', 'http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd'))}
    importXmlns = {}
    hasPreLB = hasCalLB = hasDefLB = hasRefLB = hasGenLB = False
    preLB = []
    defLB = []
    calLB = []
    refLB = []
    genLB = []

    def lbDepthList(lbStruct, depth, parentList=None):
        if (len(lbStruct) > 0):
            if ((depth == topDepth) or (not hasDepthColumn)):
                return lbStruct[(- 1)].childStruct
            return lbDepthList(lbStruct[(- 1)].childStruct, (depth - 1), list)
        else:
            if hasDepthColumn:
                cntlr.addToLog('Depth error, Excel sheet: {excelSheet} row: {excelRow}'.format(excelSheet=importSheetName, excelRow=iRow), messageCode='importExcel:depth')
            return None
    splitString = None
    importFileName = None
    importSheetNames = []
    skipRows = []
    genDocs = {}
    genElementsDoc = None

    def newDoc(name):
        genDocs[name] = PythonUtil.attrdict(name=name, initialComment=None, schemaDocumentation=None, extensionSchemaPrefix=, extensionSchemaFilename=, extensionSchemaRelDirname=None, extensionSchemaNamespaceURI=, extensionSchemaVersion=None, extensionRoles={}, extensionRoleLabels=defaultdict(set), extensionElements={}, extensionTypes={}, extensionLabels={}, extensionReferences=OrderedDefaultDict(OrderedSet), hasEnumerationDocumentation=False, imports={'xbrli': (('namespace', XbrlConst.xbrli), ('schemaLocation', 'http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd'))}, includes=[], importXmlns={}, importFilenames={}, childGenDocs=[], linkbaseRefs=[], labelLinkbases=[], referenceLinkbases=[], hasPreLB=False, hasCalLB=False, hasDefLB=False, hasRefLB=False, hasGenLB=False, generated=False)
        return genDocs[name]
    thisDoc = newDoc(None)
    excelDir = (os.path.dirname(excelFile) + os.path.sep)

    def docRelpath(filename, baseDir=None):
        if (baseDir is None):
            baseDir = thisDoc.extensionSchemaRelDirname
        if ((baseDir is not None) and (not (UrlUtil.isAbsolute(filename) or os.path.isabs(filename)))):
            return posixpath.relpath(filename, baseDir)
        return filename
    isUSGAAP = False
    isGenerateAndImport = True
    extensionPrefixForCoreLabels = None
    dtsActionColIndex = 0
    dtsFiletypeColIndex = 1
    dtsPrefixColIndex = 2
    dtsFilenameColIndex = 3
    dtsNamespaceURIColIndex = 4
    for (iRow, row) in enumerate((dtsWs.rows if dtsWs else ())):
        try:
            if (len(row) < 1):
                continue
            _col0 = row[0].value
            if (isinstance(_col0, str) and _col0.startswith('#')):
                continue
            if (iRow == 0):
                for (i, col) in enumerate(row):
                    v = xlValue(col)
                    if isinstance(v, str):
                        if (v == 'specification'):
                            dtsActionColIndex = i
                        if v.startswith('file type'):
                            dtsFiletypeColIndex = i
                        if v.startswith('prefix'):
                            dtsPrefixColIndex = i
                        if v.startswith('file, href or role definition'):
                            dtsFilenameColIndex = i
                        if v.startswith('namespace URI'):
                            dtsNamespaceURIColIndex = i
                continue
            action = filetype = prefix = filename = namespaceURI = None
            if (len(row) > dtsActionColIndex):
                action = xlValue(row[dtsActionColIndex])
            if (len(row) > dtsFiletypeColIndex):
                filetype = xlValue(row[dtsFiletypeColIndex])
            if (len(row) > dtsPrefixColIndex):
                prefix = xlValue(row[dtsPrefixColIndex])
            if (len(row) > dtsFilenameColIndex):
                filename = xlValue(row[dtsFilenameColIndex])
            if (len(row) > dtsNamespaceURIColIndex):
                namespaceURI = xlValue(row[dtsNamespaceURIColIndex])
            lbType = lang = None
            if (action == 'import'):
                if (filetype in ('role', 'arcrole')):
                    continue
                elif (filetype == 'schema'):
                    thisDoc.imports[prefix] = (('namespace', namespaceURI), ('schemaLocation', docRelpath(filename)))
                    thisDoc.importXmlns[prefix] = namespaceURI
                    thisDoc.importFilenames[prefix] = filename
                    if re.match('http://[^/]+/us-gaap/', namespaceURI):
                        isUSGAAP = True
                elif (filetype == 'linkbase'):
                    typeLang = prefix.split()
                    if (len(typeLang) > 0):
                        lbType = typeLang[0]
                    else:
                        lbType = 'unknown'
                    thisDoc.linkbaseRefs.append((lbType, filename, False))
            elif ((action == 'include') and filename):
                thisDoc.includes.append(docRelpath(filename))
            elif ((action == 'xmlns') and prefix and namespaceURI):
                thisDoc.importXmlns[prefix] = namespaceURI
            elif (action in ('extension', 'generate')):
                if (filetype == 'schema'):
                    if prefix:
                        if (not thisDoc.name):
                            del genDocs[thisDoc.name]
                        thisDoc = newDoc(prefix)
                    thisDoc.extensionSchemaPrefix = prefix
                    thisDoc.extensionSchemaFilename = filename
                    thisDoc.extensionSchemaNamespaceURI = namespaceURI
                    if ((not UrlUtil.isAbsolute(filename)) and (not os.path.isabs(filename))):
                        thisDoc.extensionSchemaRelDirname = posixpath.dirname(filename)
                    else:
                        thisDoc.extensionSchemaRelDirname = None
                elif (filetype == 'linkbase'):
                    typeLang = prefix.split()
                    if (len(typeLang) > 0):
                        lbType = typeLang[0]
                    else:
                        lbType = 'unknown'
                    if (len(typeLang) > 1):
                        lang = referenceRole = typeLang[1]
                    else:
                        lang = None
                        referenceRole = XbrlConst.standardReference
                    if (lbType in ('label', 'generic-label')):
                        thisDoc.labelLinkbases.append((lbType, lang, filename))
                        if ((action == 'extension') and (not extensionPrefixForCoreLabels)):
                            extensionPrefixForCoreLabels = thisDoc.extensionSchemaPrefix
                    elif (lbType in ('reference', 'generic-reference')):
                        hasRefLB = True
                        thisDoc.referenceLinkbases.append((lbType, referenceRole, filename))
                    elif (lbType == 'presentation'):
                        thisDoc.hasPreLB = hasPreLB = True
                    elif (lbType == 'definition'):
                        thisDoc.hasDefLB = hasDefLB = True
                    elif (lbType == 'calculation'):
                        thisDoc.hasCalLB = hasCalLB = True
                    elif (lbType == 'generic'):
                        thisDoc.hasGenLB = hasGenLB = True
                    thisDoc.linkbaseRefs.append((lbType, filename, True))
                elif ((filetype == 'initialComment') and prefix):
                    thisDoc.initialComment = prefix
                elif ((filetype == 'schemaDocumentation') and prefix):
                    thisDoc.schemaDocumentation = prefix
                elif (filetype == 'enumerationDocumentation'):
                    thisDoc.hasEnumerationDocumentation = True
                elif ((filetype == 'role') and namespaceURI):
                    thisDoc.extensionRoles[namespaceURI] = (filename, prefix)
                elif ((filetype == 'role label') and namespaceURI and prefix):
                    thisDoc.extensionRoleLabels[namespaceURI].add((filename, prefix))
                elif ((filetype == 'schema-version') and filename):
                    thisDoc.extensionSchemaVersion = filename
                elif ((filetype == 'table-style') and (filename == 'xbrl-us')):
                    isUSGAAP = True
                elif (filetype == 'elements'):
                    genElementsDoc = thisDoc
            elif ((action == 'meta') and (filetype == 'table-style') and (filename == 'xbrl-us')):
                isUSGAAP = True
            elif ((action == 'meta') and (filetype == 'generate-style') and (filename == 'import-separately')):
                isGenerateAndImport = False
            elif ((action == 'workbook') and filename):
                importFileName = filename
            elif ((action == 'worksheet') and filename):
                importSheetNames.append(filename)
            elif ((action == 'colheader') and filename and namespaceURI):
                if (namespaceURI == 'split'):
                    splitString = filename
                else:
                    importColHeaderMap[filename].append(namespaceURI)
                    if (namespaceURI not in importColumnHeaders):
                        fatalLoadingErrors.append('colheader {} definition {} not recognized.'.format(filename, namespaceURI))
            elif ((action == 'skip rows') and filename):
                (fromRow, _sep, toRow) = filename.partition('-')
                try:
                    skipRows.append((int(fromRow), (int(toRow) if toRow else int(fromRow))))
                except (ValueError, TypeError):
                    fatalLoadingErrors.append('Exception (at skip rows): {error}, Excel sheet: {excelSheet} row: {excelRow}'.format(error=err, excelSheet=dtsSheet, excelRow=iRow))
        except Exception as err:
            fatalLoadingErrors.append('Exception: {error}, Excel sheet: {excelSheet} row: {excelRow}, Traceback: {traceback}'.format(error=err, excelSheet=dtsSheet, excelRow=iRow, traceback=traceback.format_tb(sys.exc_info()[2])))
    for thisDoc in genDocs.values():
        linkbaseRefsToRemove = [i for (i, (lbType, filename, generate)) in enumerate(thisDoc.linkbaseRefs) if ((not generate) and ((lbType, filename, True) in thisDoc.linkbaseRefs))]
        while len(linkbaseRefsToRemove):
            i = linkbaseRefsToRemove.pop()
            thisDoc.linkbaseRefs.pop(i)
    dtsWs = None
    genOrder = []
    for (name, doc) in genDocs.items():
        insertPos = len(genOrder)
        for (i, otherDoc) in enumerate(genOrder):
            if (doc.name in otherDoc.imports):
                insertPos = i
                break
        genOrder.insert(insertPos, doc)
    if importFileName:
        importExcelBook = load_workbook(importFileName, read_only=True, data_only=True)
        sheetNames = importExcelBook.get_sheet_names()
    if importSheetNames:
        for importSheetName in importSheetNames:
            if (importSheetName not in sheetNames):
                fatalLoadingErrors.append('Worksheet {} specified for Excel importing, but not present in workbook.'.format(importSheetName))
    else:
        for s in sheetNames:
            if s.endswith('Concepts'):
                importSheetNames.append(s)
        if (not importSheetNames):
            for s in sheetNames:
                if (('xbrl' in s.lower()) and ('dts' not in s)):
                    importSheetNames.append(s)
        if (not importSheetNames):
            fatalLoadingErrors.append('Worksheet {} specified for Excel importing, but not present in workbook.'.format(importSheetName))
    if ((not isUSGAAP) and genOrder):
        genOrder[0].importXmlns['iod'] = 'http://disclosure.edinet-fsa.go.jp/taxonomy/common/2013-03-31/iod'
    headerCols = OrderedDict()
    headerColsAllElrs = set()
    hasLinkroleSeparateRow = True
    hasPreferredLabelTextColumn = False
    hasConceptAttributeColumn = False
    hasDepthColumn = False
    hasPresentationParentColumn = False
    hasRelationshipToCol = False
    hasrelationshipAttributeColumn = False
    headerRows = set()
    topDepth = 999999
    for importSheetName in importSheetNames:
        if (importSheetName not in sheetNames):
            continue
        headerCols.clear()
        headerRows.clear()
        hasConceptAttributeColumn = False
        hasDepthColumn = False
        hasPresentationParentColumn = False
        hasRelationshipToCol = False
        hasrelationshipAttributeColumn = False
        conceptsWs = importExcelBook[importSheetName]

        def setHeaderCols(row):
            headerCols.clear()
            for (iCol, colCell) in enumerate(row):
                v = xlValue(colCell)
                if isinstance(v, str):
                    v = v.strip()
                if (v in importColHeaderMap):
                    for hdr in importColHeaderMap[v]:
                        if (hdr in importColumnHeaders):
                            headerCols[importColumnHeaders[hdr]] = iCol
                elif (v in importColumnHeaders):
                    headerCols[importColumnHeaders[v]] = iCol
                elif isinstance(v, str):
                    if any((v.startswith(r) for r in ('label,', 'labels,', 'reference,', 'references,', 'relationship to,'))):
                        m = resourceParsePattern.match(v)
                        if m:
                            _resourceType = m.group(1)
                            _resourceRole = ('/' + m.group(2))
                            _resourceLangOrPart = m.group(4)
                            headerCols[(_resourceType, _resourceRole, _resourceLangOrPart)] = iCol
                    else:
                        headerCols[v] = iCol
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            if any(((fromRow <= (iRow + 1) <= toRow) for (fromRow, toRow) in skipRows)):
                continue
            setHeaderCols(row)
            if ((sum((1 for h in headerCols if (h in ('name', 'type', 'depth', 'periodType')))) >= 3) or (sum((1 for h in headerCols if ((h == 'name') or (isinstance(h, tuple) and (h[0] == 'relationship to'))))) >= 2)):
                headerRows.add((iRow + 1))
            if ('linkrole' in headerCols):
                hasLinkroleSeparateRow = False
            if (('preferredLabel' in headerCols) and any(((isinstance(h, tuple) and (h[0] == 'label') and (h[1] == '/preferredLabel')) for h in headerCols))):
                hasPreferredLabelTextColumn = True
            if ('depth' in headerCols):
                hasDepthColumn = True
            if ('presentationParent' in headerCols):
                hasPresentationParentColumn = True
            if ((not hasDepthColumn) and hasPresentationParentColumn):
                topDepth = 0
            hasRelationshipToCol = any(((h[0] == 'relationship to') for h in headerCols if isinstance(h, tuple)))
            headerCols.clear()

        def cellHasValue(row, header, _type):
            if (header in headerCols):
                iCol = headerCols[header]
                return ((iCol < len(row)) and isinstance(row[iCol].value, _type))
            return False

        def cellValue(row, header, strip=False, nameChars=False, default=None):
            if (header in headerCols):
                iCol = headerCols[header]
                if (iCol < len(row)):
                    v = xlValue(row[iCol])
                    if (strip and isinstance(v, str)):
                        v = v.strip()
                    if (nameChars and isinstance(v, str)):
                        v = .join((c for c in v if (c.isalnum() or (c in ('.', '_', '-')))))
                    if (v is None):
                        return default
                    return v
            return default

        def valueNameChars(v):
            return .join((c for c in v if (c.isalnum() or (c in ('.', '_', '-')))))

        def rowPrefixNameValues(row):
            prefix = cellValue(row, 'prefix', nameChars=True)
            if cellHasValue(row, 'name', str):
                if (not prefix):
                    (prefix, _sep, _name) = cellValue(row, 'name').partition(':')
                    if (not _sep):
                        prefix = 
                    name = cellValue(row, 'name', nameChars=True)[len(prefix):]
                else:
                    name = cellValue(row, 'name', nameChars=True)
            else:
                name = None
            if ((not prefix) and ('prefix' not in headerCols) and (genElementsDoc is not None)):
                prefix = genElementsDoc.extensionSchemaPrefix
            return (prefix, name)

        def checkImport(thisDoc, qname):
            (prefix, sep, localName) = qname.partition(':')
            if sep:
                if (prefix not in thisDoc.imports):
                    if (prefix == 'xbrldt'):
                        thisDoc.imports['xbrldt'] = (('namespace', XbrlConst.xbrldt), ('schemaLocation', 'http://www.xbrl.org/2005/xbrldt-2005.xsd'))
                    elif (prefix == 'nonnum'):
                        thisDoc.imports['nonnum'] = (('namespace', 'http://www.xbrl.org/dtr/type/non-numeric'), ('schemaLocation', 'http://www.xbrl.org/dtr/type/nonNumeric-2009-12-16.xsd'))
                    elif ((prefix != thisDoc.extensionSchemaPrefix) and (prefix != 'xs')):
                        cntlr.addToLog('Warning: prefix schema file is not imported for: {qname}'.format(qname=qname), messageCode='importExcel:warning', file=thisDoc.extensionSchemaFilename)
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            if ((iRow + 1) in headerRows):
                setHeaderCols(row)
                hasConceptAttributeColumn = any((v.startswith('attribute, ') for v in headerCols if isinstance(v, str)))
                hasRelationshipAttributeColumn = any((v.startswith('relationship attribute, ') for v in headerCols if isinstance(v, str)))
            elif ((not (hasLinkroleSeparateRow and ((iRow + 1) in headerRows))) and ('depth' in headerCols)):
                depth = cellValue(row, 'depth')
                if (isinstance(depth, int) and (depth < topDepth)):
                    topDepth = depth
        currentELR = currentELRdefinition = None
        for (iRow, row) in enumerate((conceptsWs.rows if conceptsWs else ())):
            useLabels = False
            eltEnumRefsParts = None
            if any(((fromRow <= (iRow + 1) <= toRow) for (fromRow, toRow) in skipRows)):
                continue
            if (all(((col.value is None) for col in row)) or all(((isinstance(row[i].value, str) and (row[i].value.strip() == 'n/a')) for i in (headerCols.get('name'), headerCols.get('type'), headerCols.get('value')) if (i is not None)))):
                continue
            try:
                isHeaderRow = ((iRow + 1) in headerRows)
                isELRrow = (hasLinkroleSeparateRow and ((iRow + 2) in headerRows))
                if isHeaderRow:
                    setHeaderCols(row)
                    headerColsAllElrs |= _DICT_SET(headerCols.keys())
                elif isELRrow:
                    currentELR = currentELRdefinition = None
                    for colCell in row:
                        v = str((xlValue(colCell) or ))
                        if v.startswith('http://'):
                            currentELR = v
                        elif ((not currentELRdefinition) and v.endswith('\u3000科目一覧')):
                            currentELRdefinition = v[0:(- 5)]
                        elif (not currentELRdefinition):
                            currentELRdefinition = v
                    if (currentELR or currentELRdefinition):
                        if hasPreLB:
                            preLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                            if hasPresentationParentColumn:
                                preRels = set()
                        if hasDefLB:
                            defLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                        if hasCalLB:
                            calLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                            calRels = set()
                        if hasGenLB:
                            genLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                elif headerCols:
                    if (('linkrole' in headerCols) and cellHasValue(row, 'linkrole', str)):
                        v = cellValue(row, 'linkrole', strip=True)
                        _trialELR = _trialELRdefinition = None
                        if v.startswith('http://'):
                            _trialELR = v
                        elif v.endswith('\u3000科目一覧'):
                            _trialELRdefinition = v[0:(- 5)]
                        else:
                            _trialELRdefinition = v
                        if ((_trialELR and (_trialELR != currentELR)) or (_trialELRdefinition and (_trialELRdefinition != currentELRdefinition))):
                            currentELR = _trialELR
                            currentELRdefinition = _trialELRdefinition
                            if (currentELR or currentELRdefinition):
                                if hasPreLB:
                                    preLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                if hasDefLB:
                                    defLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                if hasCalLB:
                                    calLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                                    calRels = set()
                                if hasGenLB:
                                    genLB.append(LBentry(role=currentELR, name=currentELRdefinition, isELR=True))
                    (prefix, name) = rowPrefixNameValues(row)
                    if cellHasValue(row, 'depth', int):
                        depth = cellValue(row, 'depth')
                    elif hasDepthColumn:
                        depth = None
                    else:
                        depth = 0
                    subsGrp = cellValue(row, 'substitutionGroup')
                    isConcept = (subsGrp in ('xbrli:item', 'xbrli:tuple', 'xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))
                    if ((prefix in genDocs) and (name not in genDocs[prefix].extensionElements) and name):
                        thisDoc = genDocs[prefix]
                        eltType = cellValue(row, 'type')
                        eltTypePrefix = cellValue(row, 'typePrefix')
                        if (not eltType):
                            eltType = 'xbrli:stringItemType'
                        elif (eltTypePrefix and (':' not in eltType)):
                            eltType = ((eltTypePrefix + ':') + eltType)
                        elif ((':' not in eltType) and eltType.endswith('ItemType')):
                            eltType = ('xbrli:' + eltType)
                        abstract = cellValue(row, 'abstract')
                        nillable = cellValue(row, 'nillable')
                        balance = cellValue(row, 'balance')
                        periodType = cellValue(row, 'periodType')
                        eltAttrs = {'name': name, 'id': (((prefix or ) + '_') + name)}
                        if eltType:
                            eltAttrs['type'] = eltType
                            checkImport(thisDoc, eltType)
                        if subsGrp:
                            eltAttrs['substitutionGroup'] = subsGrp
                            checkImport(thisDoc, subsGrp)
                        if (abstract or (subsGrp in ('xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))):
                            eltAttrs['abstract'] = (abstract or 'true')
                        if nillable:
                            eltAttrs['nillable'] = nillable
                        if balance:
                            eltAttrs['{http://www.xbrl.org/2003/instance}balance'] = balance
                        if periodType:
                            eltAttrs['{http://www.xbrl.org/2003/instance}periodType'] = periodType
                        if hasConceptAttributeColumn:
                            for header in headerCols:
                                if (isinstance(header, str) and header.startswith('attribute, ')):
                                    value = cellValue(row, header)
                                    if (value not in (None, )):
                                        eltAttrs[header[11:]] = value
                        eltFacets = None
                        eltEnumRefParts = None
                        if (eltType not in ('nonnum:domainItemType', 'xbrli:booleanItemType', 'xbrli:positiveIntegerItemType', 'xbrli:dateItemType', 'xbrli:gYearItemType')):
                            for facet in ('minLength', 'maxLength', 'minInclusive', 'maxInclusive', 'length', 'fixed', 'pattern', 'enumeration', 'excludedEnumeration'):
                                v = cellValue(row, facet)
                                if (v is not None):
                                    if ((facet == 'enumeration') and v.startswith('See tab ')):
                                        _match = re.match('See tab ([^!]+)([!]([0-9]+):([0-9]+))?', v)
                                        if _match:
                                            (_tab, _dummy, _rowFrom, _rowTo) = _match.groups()
                                            if (_tab in sheetNames):
                                                enumWs = importExcelBook[_tab]
                                                if (_rowFrom and _rowTo):
                                                    colHdrs = [enumWs.cell(row=1, column=i).value for i in range(1, (enumWs.max_column + 1))]
                                                    eltEnumValues = []
                                                    eltEnumRefsParts = []
                                                    for i in range(int(_rowFrom), (int(_rowTo) + 1)):
                                                        _parts = []
                                                        eltEnumRefsParts.append(_parts)
                                                        for (j, h) in enumerate(colHdrs):
                                                            c = enumWs.cell(row=i, column=(j + 1)).value
                                                            if (c is not None):
                                                                if (h == 'enumeration'):
                                                                    eltEnumValues.append(str(c))
                                                                else:
                                                                    m = resourceParsePattern.match(h)
                                                                    if m:
                                                                        _resourceType = m.group(1)
                                                                        _resourceRole = ('/' + m.group(2))
                                                                        _resourceLangOrPart = m.group(4)
                                                                        _parts.append(((_resourceType, _resourceRole, _resourceLangOrPart), c))
                                                    v = ('\n'.join(eltEnumValues) if eltEnumValues else None)
                                                else:
                                                    v = '\n'.join((' = '.join((xlValue(col) for col in row if xlValue(col))) for (i, row) in enumerate(enumWs.rows) if (i > 0)))
                                    if (v is not None):
                                        if (eltFacets is None):
                                            eltFacets = {}
                                        eltFacets[facet] = v
                        if (eltType and (':' in eltType)):
                            (_typePrefix, _sep, _typeName) = eltType.rpartition(':')
                            baseType = cellValue(row, 'baseType')
                            baseTypePrefix = cellValue(row, 'baseTypePrefix')
                            if (baseType and baseTypePrefix):
                                _baseType = '{}:{}'.format(baseTypePrefix, baseType)
                            elif baseType:
                                _baseType = baseType
                            elif _typeName.endswith('ItemType'):
                                _baseType = 'xbrli:tokenItemType'
                            else:
                                _baseType = 'xs:token'
                            if (_typePrefix in genDocs):
                                _typeDoc = genDocs[_typePrefix]
                                if (_typeName not in _typeDoc.extensionTypes):
                                    _typeDoc.extensionTypes[_typeName] = ({'name': _typeName, 'base': _baseType}, eltFacets)
                                thisDoc.extensionElements[name] = (eltAttrs, None)
                            else:
                                thisDoc.extensionElements[name] = (eltAttrs, eltFacets)
                        else:
                            thisDoc.extensionElements[name] = (eltAttrs, eltFacets)
                        thisDoc = None
                    useLabels = True
                    if ((depth is not None) or hasPresentationParentColumn):
                        if (name is None):
                            _label = None
                            for colCell in row:
                                if (colCell.value is not None):
                                    _label = xlValue(colCell)
                                    break
                            print('Sheet {} row {} has relationships and no "name" field, label: {}'.format(importSheetName, (iRow + 1), _label))
                        if hasPreLB:
                            preferredLabel = cellValue(row, 'preferredLabel')
                            if hasDepthColumn:
                                entryList = lbDepthList(preLB, depth)
                                if ((entryList is not None) and isConcept):
                                    if ((not name) or (not prefix)):
                                        _name = 'none'
                                    if (depth == topDepth):
                                        entryList.append(LBentry(prefix=prefix, name=name, isRoot=True))
                                    else:
                                        entryList.append(LBentry(prefix=prefix, name=name, arcrole=XbrlConst.parentChild, role=preferredLabel))
                            elif hasPresentationParentColumn:
                                preParent = cellValue(row, 'presentationParent', default=)
                                if preParent:
                                    (preParentPrefix, _sep, preParentName) = preParent.rpartition(':')
                                    preParentName = valueNameChars(preParentName)
                                    entryList = lbDepthList(preLB, topDepth)
                                    if (entryList is not None):
                                        preRel = (preParentPrefix, preParentName, prefix, name, (currentELR or currentELRdefinition))
                                        if (preRel not in preRels):
                                            entryList.append(LBentry(prefix=preParentPrefix, name=preParentName, isRoot=True, childStruct=[LBentry(prefix=prefix, name=name, arcrole=XbrlConst.parentChild, preferredLabel=preferredLabel)]))
                                            preRels.add(preRel)
                                        else:
                                            pass
                        if (hasDefLB and (topDepth != 999999)):
                            entryList = lbDepthList(defLB, depth)
                            if (entryList is not None):
                                if (depth == topDepth):
                                    if isConcept:
                                        entryList.append(LBentry(prefix=prefix, name=name, isRoot=True))
                                elif ((not preferredLabel) or (not any((((lbEntry.prefix == prefix) and (lbEntry.name == name)) for lbEntry in entryList)))):
                                    eltAttrs = {}
                                    parentLBentry = lbDepthList(defLB, (depth - 1))[(- 1)]
                                    parentName = parentLBentry.name
                                    parentEltAttrs = {}
                                    for doc in genDocs.values():
                                        if (name in doc.extensionElements):
                                            eltAttrs = doc.extensionElements.get(name, NULLENTRY)[0]
                                        if (parentName in doc.extensionElements):
                                            parentEltAttrs = doc.extensionElements.get(parentName, NULLENTRY)[0]
                                    if (isUSGAAP and (parentEltAttrs.get('substitutionGroup') == 'xbrldt:dimensionItem') and (eltAttrs.get('type') != 'nonnum:domainItemType')):
                                        typedDomainRef = ('#' + eltAttrs.get('id', ))
                                        parentEltAttrs['{http://xbrl.org/2005/xbrldt}typedDomainRef'] = typedDomainRef
                                    elif isConcept:
                                        role = None
                                        if (('dimensionDefault' in headerCols) and cellHasValue(row, 'dimensionDefault', (str, bool))):
                                            v = cellValue(row, 'dimensionDefault', strip=True)
                                            if v:
                                                role = '_dimensionDefault_'
                                        entryList.append(LBentry(prefix=prefix, name=name, arcrole='_dimensions_', role=role))
                        if hasCalLB:
                            calcParents = cellValue(row, 'calculationParent', default=).split()
                            calcWeights = str(cellValue(row, 'calculationWeight', default=)).split()
                            if (calcParents and calcWeights):
                                for (i, calcParent) in enumerate(calcParents):
                                    calcWeight = (calcWeights[i] if (i < len(calcWeights)) else calcWeights[(- 1)])
                                    (calcParentPrefix, _sep, calcParentName) = calcParent.rpartition(':')
                                    calcParentName = valueNameChars(calcParentName)
                                    entryList = lbDepthList(calLB, topDepth)
                                    if (entryList is not None):
                                        calRel = (calcParentPrefix, calcParentName, prefix, name)
                                        if (calRel not in calRels):
                                            entryList.append(LBentry(prefix=calcParentPrefix, name=calcParentName, isRoot=True, childStruct=[LBentry(prefix=prefix, name=name, arcrole=XbrlConst.summationItem, weight=calcWeight)]))
                                            calRels.add(calRel)
                                        else:
                                            pass
                    hasRelationshipToCol = any(((h[0] == 'relationship to') for h in headerCols if isinstance(h, tuple)))
                if (useLabels or hasRelationshipToCol):
                    (prefix, name) = rowPrefixNameValues(row)
                    if ((name is not None) and ((prefix in genDocs) or extensionPrefixForCoreLabels or hasRelationshipToCol)):
                        thisDoc = genDocs.get((extensionPrefixForCoreLabels or prefix))
                        preferredLabel = cellValue(row, 'preferredLabel')
                        for (colItem, iCol) in headerCols.items():
                            if isinstance(colItem, tuple):
                                colItemType = colItem[0]
                                role = colItem[1]
                                lang = part = colItem[2]
                                cell = row[iCol]
                                v = xlValue(cell)
                                if ((v is None) or (isinstance(v, str) and (not v))):
                                    values = ()
                                else:
                                    v = str(v)
                                    if (colItemType in ('label', 'reference', 'relationship to')):
                                        values = (v,)
                                    elif (colItemType in ('labels', 'references')):
                                        values = v.split('\n')
                                if (preferredLabel and ('indented' in colItem) and (not hasPreferredLabelTextColumn)):
                                    role = preferredLabel
                                for (i, value) in enumerate(values):
                                    if (colItemType == 'relationship to'):
                                        entryList = lbDepthList(genLB, topDepth)
                                        if (entryList is not None):
                                            toName = value
                                            if (':' in toName):
                                                (toPrefix, _sep, toName) = value.partition(':')
                                            else:
                                                toPrefix = prefix
                                            if hasRelationshipAttributeColumn:
                                                relAttrs = None
                                                for header in headerCols:
                                                    if (isinstance(header, str) and header.startswith('relationship attribute, ')):
                                                        attrValue = cellValue(row, header)
                                                        if (attrValue not in (None, )):
                                                            if (relAttrs is None):
                                                                relAttrs = {}
                                                            relAttrs[header[24:]] = attrValue
                                            entryList.append(LBentry(prefix=prefix, name=name, isRoot=True, childStruct=[LBentry(prefix=toPrefix, name=toName, arcrole=role, relAttrs=relAttrs)]))
                                    elif (thisDoc is None):
                                        pass
                                    elif (colItemType in ('label', 'labels')):
                                        if isConcept:
                                            if (hasPreferredLabelTextColumn and (role == '/preferredLabel')):
                                                role = preferredLabel
                                        elif (role == XbrlConst.standardLabel):
                                            role = XbrlConst.genStandardLabel
                                        elif (role == XbrlConst.documentationLabel):
                                            role = XbrlConst.genDocumentationLabel
                                        else:
                                            continue
                                        thisDoc.extensionLabels[(prefix, name, lang, role)] = value.strip()
                                    elif (hasRefLB and (colItemType == 'reference')):
                                        if isConcept:
                                            thisDoc.extensionReferences[(prefix, name, role)].add((part, value.strip()))
                                    elif (hasRefLB and (colItemType == 'references')):
                                        if isConcept:
                                            if role.endswith('#'):
                                                _role = '{}{:05.0f}'.format(role, i)
                                            else:
                                                _role = role
                                            _value = value.strip().replace('\\n', '\n')
                                            if (part is None):
                                                (_part, _sep, _value) = _value.partition(' ')
                                            else:
                                                _part = part
                                            thisDoc.extensionReferences[(prefix, name, _role)].add((_part, _value))
                        if (isConcept and eltEnumRefsParts and (thisDoc is not None)):
                            for (i, _enumRefParts) in enumerate(eltEnumRefsParts):
                                for ((colItemType, role, part), value) in _enumRefParts:
                                    if (colItemType == 'reference'):
                                        _role = '{}#{:05.0f}'.format(role, (i + 1))
                                        thisDoc.extensionReferences[(prefix, name, _role)].add((part, value.strip()))
                        thisDoc = None
            except Exception as err:
                fatalLoadingErrors.append('Excel sheet: {excelSheet}, row: {excelRow}, error: {error}, Traceback: {traceback}'.format(error=err, excelSheet=importSheetName, excelRow=iRow, traceback=traceback.format_tb(sys.exc_info()[2])))
        if (not headerCols):
            if (not conceptsWs):
                fatalLoadingErrors.append('Neither control worksheet (XBRL DTS tab) nor standard columns found, no DTS imported.')
            elif (not currentELR):
                fatalLoadingErrors.append('Extended link role not found, no DTS imported.')
    if fatalLoadingErrors:
        raise Exception(',\n '.join(fatalLoadingErrors))
    if (isUSGAAP and hasDefLB):

        def fixUsggapTableDims(lvl1Struct, level=0):
            foundTable = False
            emptyLinks = []
            foundHeadingItems = []
            foundLineItems = []
            for lvl1Entry in lvl1Struct:
                for lvl2Entry in lvl1Entry.childStruct:
                    if any((lvl2Entry.name.endswith(suffix) for suffix in ('Table', '_table', 'Cube', '_cube'))):
                        for lvl3Entry in lvl2Entry.childStruct:
                            if any((lvl3Entry.name.endswith(suffix) for suffix in ('LineItems', '_line_items'))):
                                foundLineItems.append((lvl1Entry, lvl2Entry, lvl3Entry))
                                foundTable = True
                                break
                    else:
                        foundHeadingItems.append((lvl1Entry, lvl2Entry))
                if (not foundLineItems):
                    foundNestedTable = fixUsggapTableDims(lvl1Entry.childStruct, (level + 1))
                    if ((level == 0) and (not foundNestedTable)):
                        emptyLinks.append(lvl1Entry)
                    foundTable |= foundNestedTable
                    del foundHeadingItems[:]
            for (lvl1Entry, lvl2Entry, lvl3Entry) in foundLineItems:
                i1 = lvl1Entry.childStruct.index(lvl2Entry)
                lvl1Entry.childStruct.insert(i1, lvl3Entry)
                lvl3Entry.childStruct.insert(0, lvl2Entry)
                if any((lvl1Entry.name.endswith(suffix) for suffix in ('Abstract', '_abstract', 'Root', '_root', '_package', '_heading'))):
                    lvl1Entry.childStruct.remove(lvl2Entry)
                lvl2Entry.childStruct.remove(lvl3Entry)
            for (lvl1Entry, lvl2Entry) in foundHeadingItems:
                lvl1Entry.childStruct.remove(lvl2Entry)
            for emptyLink in emptyLinks:
                lvl1Struct.remove(emptyLink)
            return foundTable
        fixUsggapTableDims(defLB)
    modelDocuments = []
    modelXbrl.blockDpmDBrecursion = True

    def generateDoc(thisDoc, parentDoc, visitedDocNames):
        if (thisDoc.name in visitedDocNames):
            modelXbrl.error('loadFromExcel:circularDependency', 'Generation order dependency is circular: %(circularDependency)s', modelXbrl=modelXbrl, circularDependency=((','.join(visitedDocNames) + ', ') + thisDoc.name))
            return
        visitedDocNames.append(thisDoc.name)
        if (XbrlConst.xsd not in thisDoc.importXmlns.values()):
            eltName = 'schema xmlns="{}"'.format(XbrlConst.xsd)
        else:
            for (k, v) in thisDoc.importXmlns.items():
                if (v == XbrlConst.xsd):
                    eltName = '{}:schema'.format(k)
                    break
        doc = createModelDocument(modelXbrl, Type.SCHEMA, thisDoc.extensionSchemaFilename, isEntry=(parentDoc is None), documentEncoding='utf-8', base=, initialXml='\n        <{eltName}\n            targetNamespace="{targetNamespace}" \n            attributeFormDefault="unqualified" \n            elementFormDefault="qualified" \n            xmlns:xs="http://www.w3.org/2001/XMLSchema" \n            xmlns:{extensionPrefix}="{targetNamespace}"\n            {importXmlns} \n            xmlns:nonnum="http://www.xbrl.org/dtr/type/non-numeric" \n            xmlns:link="http://www.xbrl.org/2003/linkbase" \n            xmlns:xbrli="http://www.xbrl.org/2003/instance" \n            xmlns:xlink="http://www.w3.org/1999/xlink" \n            xmlns:xbrldt="http://xbrl.org/2005/xbrldt" \n            {schemaVersion}{xmlLang} />\n        '.format(eltName=eltName, targetNamespace=thisDoc.extensionSchemaNamespaceURI, extensionPrefix=thisDoc.extensionSchemaPrefix, importXmlns=.join(('xmlns:{0}="{1}"\n'.format(prefix, namespaceURI) for (prefix, namespaceURI) in thisDoc.importXmlns.items())), schemaVersion=('version="{}" '.format(thisDoc.extensionSchemaVersion) if thisDoc.extensionSchemaVersion else ), xmlLang=('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else )), initialComment=thisDoc.initialComment)
        if (parentDoc is None):
            modelXbrl.modelDocument = doc
        thisDoc.generated = True
        doc.loadedFromExcel = True
        doc.inDTS = True
        doc.targetNamespace = thisDoc.extensionSchemaNamespaceURI
        schemaElt = doc.xmlRootElement
        annotationElt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'annotation')
        if thisDoc.schemaDocumentation:
            XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'documentation', text=thisDoc.schemaDocumentation)
        appinfoElt = XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'appinfo')
        appinfoElt = XmlUtil.descendant(schemaElt, XbrlConst.xsd, 'appinfo')
        for filename in thisDoc.includes:
            XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'include', attributes=(('schemaLocation', filename),))
        for (importPrefix, importAttributes) in sorted(thisDoc.imports.items(), key=(lambda item: item[1])):
            XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'import', attributes=importAttributes)
            if ((importPrefix in genDocs) and (not genDocs[importPrefix].generated)):
                generateDoc(genDocs[importPrefix], doc, visitedDocNames)
        if any(((roleURI in thisDoc.extensionRoleLabels) for roleURI in thisDoc.extensionRoles.keys())):
            for importAttributes in ((('namespace', XbrlConst.gen), ('schemaLocation', 'http://www.xbrl.org/2008/generic-link.xsd')), (('namespace', XbrlConst.genLabel), ('schemaLocation', 'http://www.xbrl.org/2008/generic-label.xsd'))):
                XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'import', attributes=importAttributes)
        _enumNum = [1]

        def addFacets(thisDoc, restrElt, facets):
            if facets:
                excludedEnumeration = facets.get('excludedEnumeration')
                if ((annotateEnumerationsDocumentation and (excludedEnumeration == 'X')) or (excludedEnumeration == 'D')):
                    for (facet, facetValue) in facets.items():
                        if (facet == 'enumeration'):
                            enumerationsDocumentation = []
                            for valLbl in facetValue.split('\n'):
                                (val, _sep, _label) = valLbl.partition('=')
                                val = val.strip()
                                if len(val):
                                    if (val == '(empty)'):
                                        val = 
                                    _label = _label.strip()
                                    enumerationsDocumentation.append(('{}: {}'.format(val, _label) if _label else val))
                            XmlUtil.addChild(XmlUtil.addChild(restrElt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=' \n'.join(enumerationsDocumentation))
                for (facet, facetValue) in sorted(facets.items(), key=(lambda i: facetSortOrder.get(i[0], i[0]))):
                    if (facet == 'enumeration'):
                        if ((not annotateEnumerationsDocumentation) and (not excludedEnumeration)):
                            for valLbl in facetValue.split('\n'):
                                (val, _sep, _label) = valLbl.partition('=')
                                val = val.strip()
                                _label = _label.strip()
                                if len(val):
                                    if (val == '(empty)'):
                                        val = 
                                    _attributes = {'value': val}
                                    if _label:
                                        _labelsByLang = None
                                        if (_label.startswith('{') and _label.endswith('}')):
                                            try:
                                                _labelsByLang = json.loads(_label)
                                            except json.decoder.JSONDecodeError:
                                                _labelsByLang = None
                                        _name = 'enum{}'.format(_enumNum[0])
                                        _attributes['id'] = ((thisDoc.extensionSchemaPrefix + '_') + _name)
                                        _enumNum[0] += 1
                                        if _labelsByLang:
                                            for (_lang, _langLabel) in _labelsByLang.items():
                                                thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, _lang, XbrlConst.genStandardLabel)] = _langLabel
                                        else:
                                            thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, defaultLabelLang, XbrlConst.genStandardLabel)] = _label
                                    enumElt = XmlUtil.addChild(restrElt, XbrlConst.xsd, facet, attributes=_attributes)
                                    if (thisDoc.hasEnumerationDocumentation and _label):
                                        if _labelsByLang:
                                            annotationElt = XmlUtil.addChild(enumElt, XbrlConst.xsd, 'annotation')
                                            for (_lang, _langLabel) in _labelsByLang.items():
                                                thisDoc.extensionLabels[(thisDoc.extensionSchemaPrefix, _name, _lang, XbrlConst.genStandardLabel)] = _langLabel
                                                XmlUtil.addChild(annotationElt, XbrlConst.xsd, 'documentation', text=_langLabel, attributes={'{http://www.w3.org/XML/1998/namespace}lang': _lang})
                                        else:
                                            XmlUtil.addChild(XmlUtil.addChild(enumElt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=_label)
                    elif (facet != 'excludedEnumeration'):
                        XmlUtil.addChild(restrElt, XbrlConst.xsd, facet, attributes={'value': str(facetValue)})
        for (eltName, eltDef) in sorted(thisDoc.extensionElements.items(), key=(lambda item: item[0])):
            (eltAttrs, eltFacets) = eltDef
            if (eltFacets and ('type' in eltAttrs)):
                eltType = eltAttrs['type']
                del eltAttrs['type']
            if any(((':' in attrname) for attrname in eltAttrs.keys())):
                for (attrname, attrvalue) in eltAttrs.copy().items():
                    if ((not attrname.startswith('{')) and (':' in attrname)):
                        del eltAttrs[attrname]
                        eltAttrs[schemaElt.prefixedNameQname(attrname).clarkNotation] = attrvalue
            isConcept = (eltAttrs.get('substitutionGroup') in ('xbrli:item', 'xbrli:tuple', 'xbrldt:hypercubeItem', 'xbrldt:dimensionItem'))
            elt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'element', attributes=eltAttrs)
            if annotateElementDocumentation:
                for labelRole in (XbrlConst.documentationLabel, XbrlConst.genDocumentationLabel):
                    labelKey = (thisDoc.extensionSchemaPrefix, eltAttrs['name'], defaultLabelLang, labelRole)
                    if (labelKey in thisDoc.extensionLabels):
                        XmlUtil.addChild(XmlUtil.addChild(elt, XbrlConst.xsd, 'annotation'), XbrlConst.xsd, 'documentation', text=thisDoc.extensionLabels[labelKey])
                        break
            if ((elt is not None) and eltFacets and isConcept):
                cmplxType = XmlUtil.addChild(elt, XbrlConst.xsd, 'complexType')
                cmplxCont = XmlUtil.addChild(cmplxType, XbrlConst.xsd, 'simpleContent')
                restrElt = XmlUtil.addChild(cmplxCont, XbrlConst.xsd, 'restriction', attributes={'base': eltType})
                addFacets(thisDoc, restrElt, eltFacets)
                del eltType
        for (roleURI, (roleDefinition, usedOnRoles)) in sorted(thisDoc.extensionRoles.items(), key=(lambda rd: rd[1])):
            roleElt = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'roleType', attributes=(('roleURI', roleURI), ('id', ('roleType_' + roleURI.rpartition('/')[2]))))
            if roleDefinition:
                XmlUtil.addChild(roleElt, XbrlConst.link, 'definition', text=roleDefinition)
            if usedOnRoles:
                for usedOnRole in usedOnRoles.split():
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text=usedOnRole)
            else:
                if (hasPreLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in preLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:presentationLink')
                if (hasDefLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in defLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:definitionLink')
                if (hasCalLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in calLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text='link:calculationLink')
                if (hasGenLB and any(((e.childStruct and e.isELR and ((e.role == roleURI) or (e.name == roleDefinition))) for e in genLB))):
                    XmlUtil.addChild(roleElt, XbrlConst.link, 'usedOn', text=qname('{http://xbrl.org/2008/generic}genlink:link'))
        if any(((roleURI in thisDoc.extensionRoleLabels) for roleURI in thisDoc.extensionRoles.keys())):
            genLabLB = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'linkbase')
            XmlUtil.addChild(genLabLB, XbrlConst.link, 'roleRef', attributes=(('roleURI', XbrlConst.genStandardLabel), ('{http://www.w3.org/1999/xlink}href', 'http://www.xbrl.org/2008/generic-label.xsd#standard-label'), ('{http://www.w3.org/1999/xlink}type', 'simple')))
            XmlUtil.addChild(genLabLB, XbrlConst.link, 'arcroleRef', attributes=(('arcroleURI', elementLabel), ('{http://www.w3.org/1999/xlink}href', 'http://www.xbrl.org/2008/generic-label.xsd#element-label'), ('{http://www.w3.org/1999/xlink}type', 'simple')))
            linkElt = XmlUtil.addChild(genLabLB, qname('{http://xbrl.org/2008/generic}genlink:link'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            for (roleURI, _defLabel) in sorted(thisDoc.extensionRoles.items(), key=(lambda rd: rd[0])):
                if (roleURI in thisDoc.extensionRoleLabels):
                    xlLabel = roleURI.rpartition('/')[2]
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', ('#roleType_' + xlLabel)), ('{http://www.w3.org/1999/xlink}label', ('loc_' + xlLabel))))
                    XmlUtil.addChild(linkElt, XbrlConst.qnGenArc, attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', elementLabel), ('{http://www.w3.org/1999/xlink}from', ('loc_' + xlLabel)), ('{http://www.w3.org/1999/xlink}to', ('label_' + xlLabel))))
                    for (text, lang) in thisDoc.extensionRoleLabels[roleURI]:
                        XmlUtil.addChild(linkElt, qname('{http://xbrl.org/2008/label}genlabel:label'), attributes=(('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + xlLabel)), ('{http://www.w3.org/1999/xlink}role', XbrlConst.genStandardLabel), ('{http://www.w3.org/XML/1998/namespace}lang', lang)), text=text)

        def addLinkbaseRef(lbType, lbFilename, lbDoc):
            role = 'http://www.xbrl.org/2003/role/{0}LinkbaseRef'.format(lbType)
            lbRefElt = XmlUtil.addChild(appinfoElt, XbrlConst.link, 'linkbaseRef', attributes=((('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', docRelpath(lbFilename, thisDoc.extensionSchemaRelDirname)), ('{http://www.w3.org/1999/xlink}arcrole', 'http://www.w3.org/1999/xlink/properties/linkbase')) + (() if lbType.startswith('generic') else (('{http://www.w3.org/1999/xlink}role', role),))))
            if lbDoc:
                doc.referencesDocument[lbDoc] = ModelDocumentReference('href', lbRefElt)
        for (lbRefType, filename, generate) in thisDoc.linkbaseRefs:
            if (not generate):
                for otherGenDoc in genDocs.values():
                    if ((not otherGenDoc.generated) and any((((_otherLbRefType == lbRefType) and (_otherFilename == filename) and _otherGenerate) for (_otherLbRefType, _otherFilename, _otherGenerate) in otherGenDoc.linkbaseRefs))):
                        generateDoc(otherGenDoc, doc, visitedDocNames)
                addLinkbaseRef(lbRefType, filename, None)
        doc.schemaDiscover(schemaElt, False, thisDoc.extensionSchemaNamespaceURI)
        for (typeName, typeDef) in sorted(thisDoc.extensionTypes.items(), key=(lambda item: item[0])):
            if (qname(thisDoc.extensionSchemaNamespaceURI, typeName) in modelXbrl.qnameTypes):
                continue
            (typeAttrs, typeFacets) = typeDef
            if (typeName.endswith('ItemType') or typeAttrs.get('base', ).endswith('ItemType')):
                cmplxType = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'complexType', attributes={'name': typeAttrs['name']})
                contElt = XmlUtil.addChild(cmplxType, XbrlConst.xsd, 'simpleContent')
            else:
                contElt = XmlUtil.addChild(schemaElt, XbrlConst.xsd, 'simpleType', attributes={'name': typeAttrs['name']})
            restrElt = XmlUtil.addChild(contElt, XbrlConst.xsd, 'restriction', attributes={'base': typeAttrs['base']})
            baseQn = qname(schemaElt, typeAttrs.get('base'))
            if typeFacets:
                if (baseQn and (baseQn.namespaceURI not in (XbrlConst.xsd, XbrlConst.xbrli)) and (baseQn in modelXbrl.qnameTypes)):
                    baseTypeFacets = (modelXbrl.qnameTypes[baseQn].facets or ())
                    typeFacets = dict(((facet, value) for (facet, value) in typeFacets.items() if ((facet not in baseTypeFacets) or (str(baseTypeFacets[facet]) != value))))
                addFacets(thisDoc, restrElt, typeFacets)
        extLabelRoles = {}
        extReferenceRoles = {}
        extReferenceParts = {}
        extReferenceSchemaDocs = {}
        extUnrecognizedRoles = set()
        relationshipArcroles = {}
        relationshipArcqnames = {}

        def setExtRefPart(partLocalName):
            if (partLocalName not in extReferenceParts):
                for partConcept in modelXbrl.nameConcepts.get(partLocalName, ()):
                    if ((partConcept is not None) and (partConcept.subGroupHeadQname == qnLinkPart)):
                        extReferenceParts[partLocalName] = partConcept.qname
                        extReferenceSchemaDocs[partConcept.qname.namespaceURI] = (partConcept.modelDocument.uri if partConcept.modelDocument.uri.startswith('http://') else partConcept.modelDocument.basename)
                        break
        for _headerColKey in headerColsAllElrs:
            if (isinstance(_headerColKey, tuple) and (len(_headerColKey) >= 3) and (not _headerColKey[1].startswith('http://'))):
                _resourceType = _headerColKey[0]
                _resourceRole = _headerColKey[1]
                _resourceLangOrPart = _headerColKey[2]
            elif (isinstance(_headerColKey, str) and ('!reference' in _headerColKey)):
                m = resourceParsePattern.match(_headerColKey.partition('!')[2])
                _resourceType = m.group(1)
                _resourceRole = ('/' + m.group(2))
                _resourceLangOrPart = m.group(4)
            else:
                continue
            (_resourceQName, _standardRoles) = {'label': (qnLinkLabel, standardLabelRoles), 'labels': (qnLinkLabel, standardLabelRoles), 'reference': (qnLinkReference, standardReferenceRoles), 'references': (qnLinkReference, standardReferenceRoles)}.get(_resourceType, (None, ()))
            _resourceRoleURI = None
            for _roleURI in _standardRoles:
                if _roleURI.endswith(_resourceRole):
                    _resourceRoleURI = _roleURI
                    _resourceRoleMatchPart = _resourceRole
                    break
            if (_resourceRoleURI is None):
                _resourceRoleMatchPart = _resourceRole.partition('#')[0]
                for _roleURI in modelXbrl.roleTypes:
                    if _roleURI.endswith(_resourceRoleMatchPart):
                        for _roleType in modelXbrl.roleTypes[_roleURI]:
                            if (_resourceQName in _roleType.usedOns):
                                _resourceRoleURI = _roleURI
                                break
            if (_resourceType in ('label', 'labels')):
                if _resourceRoleURI:
                    extLabelRoles[_resourceRoleMatchPart] = _resourceRoleURI
                elif any(((_resourceRoleMatchPart == k[2]) for k in thisDoc.extensionLabels.keys())):
                    modelXbrl.error('loadFromExcel:labelResourceRole', 'Label resource role not found: %(role)s', modelXbrl=modelXbrl, role=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
            elif (_resourceType in ('reference', 'references')):
                if _resourceRoleURI:
                    extReferenceRoles[_resourceRoleMatchPart] = _resourceRoleURI
                    setExtRefPart(_resourceLangOrPart)
                elif any(((_resourceRoleMatchPart == k[2]) for k in thisDoc.extensionReferences.keys())):
                    modelXbrl.error('loadFromExcel:referenceResourceRole', 'Reference resource role not found: %(role)s', modelXbrl=modelXbrl, role=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
            elif (_resourceType == 'relationship to'):
                for _arcroleURI in modelXbrl.arcroleTypes:
                    if _arcroleURI.endswith(_resourceRoleMatchPart):
                        for _arcroleType in modelXbrl.arcroleTypes[_arcroleURI]:
                            for _resourceQName in _arcroleType.usedOns:
                                break
                        break
                if (_resourceQName is None):
                    modelXbrl.error('loadFromExcel:relationshipArcrole', 'Relationship arcrole not found: %(arcrole)s', modelXbrl=modelXbrl, arcrole=_resourceRoleMatchPart, filename=thisDoc.extensionSchemaNamespaceURI)
                else:
                    relationshipArcroles[_resourceRoleMatchPart] = _arcroleURI
                    relationshipArcqnames[_arcroleURI] = _resourceQName
        for (lbType, lang, filename) in thisDoc.labelLinkbases:
            thisDoc.thisLBdir = posixpath.dirname(filename)
            langPattern = re.compile((lang or '.*'))
            _isGeneric = lbType.startswith('generic')
            if (_isGeneric and ('http://xbrl.org/2008/label' not in modelXbrl.namespaceDocs)):
                ModelDocument.load(modelXbrl, 'http://www.xbrl.org/2008/generic-link.xsd', isDiscovered=True)
                ModelDocument.load(modelXbrl, 'http://www.xbrl.org/2008/generic-label.xsd', isDiscovered=True)
            lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base=, initialXml='\n            <linkbase \n                xmlns="http://www.xbrl.org/2003/linkbase" \n                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                xmlns:xlink="http://www.w3.org/1999/xlink" \n                xmlns:xbrli="http://www.xbrl.org/2003/instance"\n                {}\n                xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}" \n                {}>{}</linkbase>\n            '.format(('\n                xmlns:genlink="http://xbrl.org/2008/generic"\n                xmlns:genlabel="http://xbrl.org/2008/label"\n                ' if _isGeneric else ), ('\n                http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                http://xbrl.org/2008/label  http://www.xbrl.org/2008/generic-label.xsd\n                ' if _isGeneric else ), ('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else ), ('\n                <arcroleRef arcroleURI="http://xbrl.org/arcrole/2008/element-label" xlink:href="http://www.xbrl.org/2008/generic-label.xsd#element-label" xlink:type="simple"/>\n                ' if _isGeneric else )), initialComment=thisDoc.initialComment)
            lbDoc.inDTS = True
            lbDoc.loadedFromExcel = True
            if isGenerateAndImport:
                addLinkbaseRef(lbType, filename, lbDoc)
            lbElt = lbDoc.xmlRootElement
            linkElt = XmlUtil.addChild(lbElt, (gen if _isGeneric else link), ('link' if _isGeneric else 'labelLink'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            firstLinkElt = linkElt
            locs = set()
            roleRefs = set()
            for (labelKey, text) in thisDoc.extensionLabels.items():
                (prefix, name, labelLang, role) = labelKey
                labelLang = (labelLang or defaultLabelLang)
                role = role.partition('#')[0]
                role = extLabelRoles.get(role, role)
                if (langPattern.match(labelLang) and (_isGeneric == (role in (XbrlConst.genStandardLabel, XbrlConst.genDocumentationLabel)))):
                    locLabel = ((prefix + '_') + name)
                    if (locLabel not in locs):
                        locs.add(locLabel)
                        XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', LBHref(thisDoc, prefix, name)), ('{http://www.w3.org/1999/xlink}label', locLabel)))
                        XmlUtil.addChild(linkElt, (gen if _isGeneric else link), ('arc' if _isGeneric else 'labelArc'), attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', (elementLabel if _isGeneric else conceptLabel)), ('{http://www.w3.org/1999/xlink}from', locLabel), ('{http://www.w3.org/1999/xlink}to', ('label_' + locLabel)), ('order', 1.0)))
                    XmlUtil.addChild(linkElt, (XbrlConst.genLabel if _isGeneric else XbrlConst.link), 'label', attributes=((('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + locLabel)), ('{http://www.w3.org/1999/xlink}role', role)) + ((('{http://www.w3.org/XML/1998/namespace}lang', labelLang),) if (True or (lang != saveXmlLang)) else ())), text=text)
                    if role:
                        if (role in XbrlConst.standardLabelRoles):
                            pass
                        elif (role in modelXbrl.roleTypes):
                            roleType = modelXbrl.roleTypes[role][0]
                            roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                        elif role.startswith('http://www.xbrl.org/2009/role/negated'):
                            roleRefs.add(('roleRef', role, ('http://www.xbrl.org/lrr/role/negated-2009-12-16.xsd#' + role.rpartition('/')[2])))
                        else:
                            extUnrecognizedRoles.add(role)
            for (roleref, roleURI, href) in roleRefs:
                XmlUtil.addChild(lbElt, XbrlConst.link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', href)), beforeSibling=firstLinkElt)
            lbDoc.linkbaseDiscover(lbElt)
            if extUnrecognizedRoles:
                modelXbrl.error('loadFromExcel:undefinedLabelRole', 'Label roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                extUnrecognizedRoles.clear()
        for (lbType, referenceRole, filename) in thisDoc.referenceLinkbases:
            thisDoc.thisLBdir = posixpath.dirname(filename)
            _isGeneric = lbType.startswith('generic')
            lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base=, initialXml='\n            <linkbase \n                xmlns="http://www.xbrl.org/2003/linkbase" \n                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                xmlns:xlink="http://www.w3.org/1999/xlink" \n                xmlns:xbrli="http://www.xbrl.org/2003/instance"\n                {}\n                xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}{}" \n                {}>{}</linkbase>\n            '.format(('\n                xmlns:genlink="http://xbrl.org/2008/generic"\n                xmlns:genreference="http://xbrl.org/2008/rerference"\n                ' if _isGeneric else ), .join([' {} {}'.format(_ns, _uri) for (_ns, _uri) in extReferenceSchemaDocs.items()]), ('\n                http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                http://xbrl.org/2008/reference  http://www.xbrl.org/2008/generic-reference.xsd\n                ' if _isGeneric else ), ('\n xml:lang="{}"'.format(saveXmlLang) if saveXmlLang else ), ('\n                <roleRef roleURI="http://www.xbrl.org/2008/role/label" xlink:href="http://www.xbrl.org/2008/generic-label.xsd#standard-label" xlink:type="simple"/>\n                <arcroleRef arcroleURI="http://xbrl.org/arcrole/2008/element-reference" xlink:href="http://xbrl.org/2008/generic-reference.xsd#element-reference" xlink:type="simple"/>\n                ' if _isGeneric else )), initialComment=thisDoc.initialComment)
            lbDoc.inDTS = True
            lbDoc.loadedFromExcel = True
            if isGenerateAndImport:
                addLinkbaseRef(lbType, filename, lbDoc)
            lbElt = lbDoc.xmlRootElement
            linkElt = XmlUtil.addChild(lbElt, (XbrlConst.gen if _isGeneric else XbrlConst.link), ('link' if _isGeneric else 'referenceLink'), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', defaultLinkRole)))
            firstLinkElt = linkElt
            locs = set()
            roleRefs = set()
            undefinedReferenceParts = set()
            for (referenceKey, references) in thisDoc.extensionReferences.items():
                (prefix, name, role) = referenceKey
                role = role.partition('#')[0]
                role = extReferenceRoles.get(role, role)
                if fnmatch(role, referenceRole):
                    locLabel = ((prefix + '_') + name)
                    if (locLabel not in locs):
                        locs.add(locLabel)
                        order = 1
                    else:
                        for order in range(2, 1000):
                            _locLabel = '{}_{}'.format(locLabel, order)
                            if (_locLabel not in locs):
                                locLabel = _locLabel
                                locs.add(locLabel)
                                break
                        if (order > 999):
                            print('resource order de-duplicate failure, too many reference parts')
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', LBHref(thisDoc, prefix, name)), ('{http://www.w3.org/1999/xlink}label', locLabel)))
                    XmlUtil.addChild(linkElt, XbrlConst.link, 'referenceArc', attributes=(('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', conceptReference), ('{http://www.w3.org/1999/xlink}from', locLabel), ('{http://www.w3.org/1999/xlink}to', ('label_' + locLabel)), ('order', order)))
                    referenceResource = XmlUtil.addChild(linkElt, (XbrlConst.genReference if _isGeneric else XbrlConst.link), 'reference', attributes=(('{http://www.w3.org/1999/xlink}type', 'resource'), ('{http://www.w3.org/1999/xlink}label', ('label_' + locLabel)), ('{http://www.w3.org/1999/xlink}role', role)))
                    for (part, text) in references:
                        setExtRefPart(part)
                        if (part in extReferenceParts):
                            partQn = extReferenceParts.get(part, part)
                            XmlUtil.addChild(referenceResource, partQn, text=text)
                        else:
                            undefinedReferenceParts.add(part)
                    if role:
                        if (role in XbrlConst.standardLabelRoles):
                            pass
                        elif (role in modelXbrl.roleTypes):
                            roleType = modelXbrl.roleTypes[role][0]
                            roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                        elif role.startswith('http://www.xbrl.org/2009/role/negated'):
                            roleRefs.add(('roleRef', role, ('http://www.xbrl.org/lrr/role/negated-2009-12-16.xsd#' + role.rpartition('/')[2])))
                        else:
                            extUnrecognizedRoles.add(role)
            for part in sorted(undefinedReferenceParts):
                print('reference part not defined: {}'.format(part))
            for (roleref, roleURI, href) in roleRefs:
                XmlUtil.addChild(lbElt, XbrlConst.link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', href)), beforeSibling=firstLinkElt)
            lbDoc.linkbaseDiscover(lbElt)
            if extUnrecognizedRoles:
                modelXbrl.error('loadFromExcel:undefinedReferenceRole', 'Reference roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                extUnrecognizedRoles.clear()
        prefixedNamespaces = modelXbrl.prefixedNamespaces

        def hrefConcept(prefix, name):
            qn = qname(prefixedNamespaces[prefix], name)
            if (qn in modelXbrl.qnameConcepts):
                return modelXbrl.qnameConcepts[qn]
            elif (name in modelXbrl.nameConcepts):
                return modelXbrl.nameConcepts[name][0]
            if (prefix not in prefixedNamespaces):
                modelXbrl.error('loadFromExcel:undefinedRelationshipElementPrefix', 'Prefix not defined: %(prefix)s', modelXbrl=modelXbrl, prefix=prefix)
                return None
            modelXbrl.error('loadFromExcel:undefinedRelationshipElement', 'QName not defined: %(prefix)s:%(localName)s', modelXbrl=modelXbrl, prefix=prefix, localName=name)
            return None

        def prefixedNameQName(prefixedName):
            if (':' not in prefixedName):
                return prefixedName
            (prefix, _sep, name) = prefixedName.rpartition(':')
            if (prefix not in prefixedNamespaces):
                modelXbrl.error('loadFromExcel:undefinedRelationshipAttributePrefix', 'Prefix not defined: %(prefix)s', modelXbrl=modelXbrl, prefix=prefix)
                return prefixedName
            return QName(prefix, prefixedNamespaces[prefix], name)

        def lbTreeWalk(lbType, parentElt, lbStruct, roleRefs, dimDef=False, locs=None, arcsFromTo=None, fromPrefix=None, fromName=None):
            order = 1.0
            for lbEntry in lbStruct:
                if lbEntry.isELR:
                    if (not lbEntry.childStruct):
                        continue
                    role = 'unspecified'
                    if (lbEntry.role and lbEntry.role.startswith('http://')):
                        role = lbEntry.role
                    elif lbEntry.name:
                        for (linkroleUri, modelRoleTypes) in modelXbrl.roleTypes.items():
                            definition = modelRoleTypes[0].definition
                            if ((lbEntry.name == definition) and (linkroleUri in thisDoc.extensionRoles)):
                                role = linkroleUri
                                break
                    if (role == 'unspecified'):
                        continue
                    if (role not in thisDoc.extensionRoles):
                        continue
                    if (role == XbrlConst.defaultLinkRole):
                        pass
                    elif (role in thisDoc.extensionRoles):
                        roleRefs.add(('roleRef', role, ((doc.uri + '#roleType_') + role.rpartition('/')[2])))
                    elif (role in modelXbrl.roleTypes):
                        roleType = modelRoleTypes[0]
                        roleRefs.add(('roleRef', role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                    else:
                        extUnrecognizedRoles.add(role)
                    linkElt = XmlUtil.addChild(parentElt, (XbrlConst.gen if (lbType == 'generic') else XbrlConst.link), ('link' if (lbType == 'generic') else (lbType + 'Link')), attributes=(('{http://www.w3.org/1999/xlink}type', 'extended'), ('{http://www.w3.org/1999/xlink}role', role)))
                    locs = set()
                    arcsFromTo = set()
                    lbTreeWalk(lbType, linkElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo)
                else:
                    toPrefix = lbEntry.prefix
                    toName = lbEntry.name
                    toHref = LBHref(thisDoc, toPrefix, toName)
                    if (toHref is None):
                        modelXbrl.error('loadFromExcel:invalidQName', "%(linkbase)s relationship element with prefix '%(prefix)s' localName '%(localName)s' not found", modelXbrl=modelXbrl, linkbase=lbType, prefix=lbEntry.prefix, localName=lbEntry.name)
                        continue
                    if ((not toPrefix) and (toName in modelXbrl.nameConcepts)):
                        toPrefix = modelXbrl.nameConcepts[toName][0].qname.prefix
                    toLabel = '{}_{}'.format(toPrefix, toName)
                    toLabelAlt = None
                    if (not lbEntry.isRoot):
                        if ((not fromPrefix) and (fromName in modelXbrl.nameConcepts)):
                            fromPrefix = modelXbrl.nameConcepts[fromName][0].qname.prefix
                        fromLabel = '{}_{}'.format(fromPrefix, fromName)
                        if ((fromLabel, toLabel) in arcsFromTo):
                            for i in range(1, 1000):
                                toLabelAlt = '{}_{}'.format(toLabel, i)
                                if ((fromLabel, toLabelAlt) not in arcsFromTo):
                                    toLabel = toLabelAlt
                                    break
                    if (((toHref not in locs) or toLabelAlt) and (not dimDef)):
                        XmlUtil.addChild(parentElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', toHref), ('{http://www.w3.org/1999/xlink}label', toLabel)))
                        locs.add(toHref)
                    if (not lbEntry.isRoot):
                        arcsFromTo.add((fromLabel, toLabel))
                        if ((lbType == 'calculation') and (lbEntry.weight is not None)):
                            otherAttrs = (('weight', lbEntry.weight),)
                        elif ((lbType == 'presentation') and lbEntry.role):
                            if (not lbEntry.role.startswith('http://')):
                                _labelRoleMatchPart = ('/' + lbEntry.role)
                                for _roleURI in modelXbrl.roleTypes:
                                    if _roleURI.endswith(_labelRoleMatchPart):
                                        for _roleType in modelXbrl.roleTypes[_roleURI]:
                                            if (XbrlConst.qnLinkLabel in _roleType.usedOns):
                                                lbEntry.role = _roleURI
                                                break
                            if (not lbEntry.role.startswith('http://')):
                                lbEntry.role = ('http://www.xbrl.org/2003/role/' + lbEntry.role)
                            otherAttrs = (('preferredLabel', lbEntry.role),)
                            if (lbEntry.role and (lbEntry.role not in XbrlConst.standardLabelRoles)):
                                if (lbEntry.role in modelXbrl.roleTypes):
                                    roleType = modelXbrl.roleTypes[lbEntry.role][0]
                                    roleRefs.add(('roleRef', lbEntry.role, ((roleType.modelDocument.uri + '#') + roleType.id)))
                                else:
                                    extUnrecognizedRoles.add(lbEntry.role)
                        elif ((lbType == 'generic') and lbEntry.arcrole):
                            if (not lbEntry.arcrole.startswith('http://')):
                                for _arcroleURI in modelXbrl.arcroleTypes:
                                    if _arcroleURI.endswith(lbEntry.arcrole):
                                        lbEntry.arcrole = _arcroleURI
                                        break
                            otherAttrs = tuple(((prefixedNameQName(_key), _value) for (_key, _value) in (lbEntry.relAttrs.items() if (lbEntry.relAttrs is not None) else ())))
                        else:
                            otherAttrs = ()
                        if (lbEntry.arcrole == '_dimensions_'):
                            fromConcept = hrefConcept(fromPrefix, fromName)
                            toConcept = hrefConcept(toPrefix, toName)
                            if dimDef:
                                if ((lbEntry.role != '_dimensionDefault_') and (not lbTreeHasDimDefault(lbEntry.childStruct))):
                                    continue
                                if ((toConcept is not None) and (toConcept.isDimensionItem or (lbEntry.role == '_dimensionDefault_'))):
                                    if ((toHref not in locs) or toLabelAlt):
                                        XmlUtil.addChild(parentElt, XbrlConst.link, 'loc', attributes=(('{http://www.w3.org/1999/xlink}type', 'locator'), ('{http://www.w3.org/1999/xlink}href', toHref), ('{http://www.w3.org/1999/xlink}label', toLabel)))
                                        locs.add(toHref)
                                    if (lbEntry.role != '_dimensionDefault_'):
                                        lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, toPrefix, toName)
                                    else:
                                        XmlUtil.addChild(parentElt, XbrlConst.link, 'definitionArc', attributes=((('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', XbrlConst.dimensionDefault), ('{http://www.w3.org/1999/xlink}from', fromLabel), ('{http://www.w3.org/1999/xlink}to', toLabel), ('order', order)) + otherAttrs))
                                        order += 1.0
                                else:
                                    lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, fromPrefix, fromName)
                                continue
                            elif ((toConcept is not None) and toConcept.isHypercubeItem):
                                arcrole = XbrlConst.all
                                otherAttrs += ((XbrlConst.qnXbrldtContextElement, 'segment'), (qnXbrldtClosed, 'true'))
                            elif ((toConcept is not None) and toConcept.isDimensionItem):
                                arcrole = XbrlConst.hypercubeDimension
                            elif ((fromConcept is not None) and fromConcept.isDimensionItem):
                                arcrole = XbrlConst.dimensionDomain
                            else:
                                arcrole = XbrlConst.domainMember
                        else:
                            arcrole = lbEntry.arcrole
                        if (arcrole in relationshipArcqnames):
                            arcqname = relationshipArcqnames[arcrole]
                            arcNS = arcqname.namespaceURI
                            arcLocalname = arcqname.localName
                        elif (lbType == 'generic'):
                            arcNS = XbrlConst.gen
                            arcLocalname = 'arc'
                        else:
                            arcNS = XbrlConst.link
                            arcLocalname = (lbType + 'Arc')
                        XmlUtil.addChild(parentElt, arcNS, arcLocalname, attributes=((('{http://www.w3.org/1999/xlink}type', 'arc'), ('{http://www.w3.org/1999/xlink}arcrole', arcrole), ('{http://www.w3.org/1999/xlink}from', fromLabel), ('{http://www.w3.org/1999/xlink}to', toLabel), ('order', order)) + otherAttrs))
                        order += 1.0
                    if ((lbType != 'calculation') or lbEntry.isRoot):
                        lbTreeWalk(lbType, parentElt, lbEntry.childStruct, roleRefs, dimDef, locs, arcsFromTo, toPrefix, toName)

        def lbTreeHasDimDefault(lbStruct):
            for lbEntry in lbStruct:
                if lbEntry.isELR:
                    if (not lbEntry.childStruct):
                        continue
                    if lbTreeHasDimDefault(lbEntry.childStruct):
                        return True
                else:
                    if ((not lbEntry.isRoot) and ((lbEntry.arcrole == '_dimensions_') and (lbEntry.role == '_dimensionDefault_'))):
                        return True
                    if lbTreeHasDimDefault(lbEntry.childStruct):
                        return True
            return False
        for (hasLB, lbType, lbLB) in (((hasPreLB and thisDoc.hasPreLB), 'presentation', preLB), ((hasDefLB and thisDoc.hasDefLB), 'definition', defLB), ((hasCalLB and thisDoc.hasCalLB), 'calculation', calLB), ((hasGenLB and thisDoc.hasGenLB), 'generic', genLB)):
            if hasLB:
                for (lbRefType, filename, generate) in thisDoc.linkbaseRefs:
                    thisDoc.thisLBdir = posixpath.dirname(filename)
                    if (generate and (lbType == lbRefType)):
                        lbDoc = ModelDocument.create(modelXbrl, ModelDocument.Type.LINKBASE, filename, base=, initialXml='\n                        <linkbase \n                            xmlns="http://www.xbrl.org/2003/linkbase" \n                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n                            xmlns:xlink="http://www.w3.org/1999/xlink" \n                            xmlns:xbrli="http://www.xbrl.org/2003/instance"{}\n                            xsi:schemaLocation="http://www.xbrl.org/2003/linkbase \n                            http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd{}" \n                            />\n                        '.format(('\n                        xmlns:generic="http://xbrl.org/2008/generic"\n                        ' if (lbType == 'generic') else ), ('\n                        http://xbrl.org/2008/generic  http://www.xbrl.org/2008/generic-link.xsd\n                        ' if (lbType == 'generic') else )), initialComment=thisDoc.initialComment)
                        lbDoc.inDTS = True
                        lbDoc.loadedFromExcel = True
                        addLinkbaseRef(lbRefType, filename, lbDoc)
                        lbElt = lbDoc.xmlRootElement
                        roleRefs = set()
                        if (lbType == 'definition'):
                            roleRefs.update((('arcroleRef', XbrlConst.all, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#all'), ('arcroleRef', XbrlConst.dimensionDefault, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#dimension-default'), ('arcroleRef', XbrlConst.dimensionDomain, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#dimension-domain'), ('arcroleRef', XbrlConst.domainMember, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#domain-member'), ('arcroleRef', XbrlConst.hypercubeDimension, 'http://www.xbrl.org/2005/xbrldt-2005.xsd#hypercube-dimension')))
                        elif (lbType == 'generic'):
                            for _arcroleURI in relationshipArcroles.values():
                                for _arcroleType in modelXbrl.arcroleTypes[_arcroleURI]:
                                    roleRefs.add(('arcroleRef', _arcroleURI, ((_arcroleType.modelDocument.uri + '#') + _arcroleType.id)))
                                    break
                        lbTreeWalk(lbType, lbElt, lbLB, roleRefs)
                        if ((lbType == 'definition') and lbTreeHasDimDefault(lbLB)):
                            lbTreeWalk(lbType, lbElt, lbLB, roleRefs, dimDef=True)
                        firstLinkElt = None
                        for firstLinkElt in lbElt.iterchildren():
                            break
                        for (roleref, roleURI, href) in roleRefs:
                            XmlUtil.addChild(lbElt, link, roleref, attributes=((('arcroleURI' if (roleref == 'arcroleRef') else 'roleURI'), roleURI), ('{http://www.w3.org/1999/xlink}type', 'simple'), ('{http://www.w3.org/1999/xlink}href', docRelpath(href, thisDoc.thisLBdir))), beforeSibling=firstLinkElt)
                        lbDoc.linkbaseDiscover(lbElt)
                        break
                if extUnrecognizedRoles:
                    modelXbrl.error('loadFromExcel:undefinedRole', '%(lbType)s linkbase roles not defined: %(undefinedRoles)s', modelXbrl=modelXbrl, lbType=lbType, undefinedRoles=','.join(sorted(extUnrecognizedRoles)))
                    extUnrecognizedRoles.clear()
        visitedDocNames.pop()

    def LBHref(thisDoc, prefix, name):
        if ((not prefix) and (name in modelXbrl.nameConcepts)):
            _concept = modelXbrl.nameConcepts[name][0]
            filename = _concept.modelDocument.uri
            prefix = _concept.qname.prefix
        elif (prefix == thisDoc.extensionSchemaPrefix):
            filename = thisDoc.extensionSchemaFilename
        elif (prefix in thisDoc.importFilenames):
            filename = thisDoc.importFilenames[prefix]
        elif (prefix in genDocs):
            doc = genDocs[prefix]
            if (not doc.generated):
                generateDoc(doc, thisDoc)
            if doc.generated:
                filename = doc.extensionSchemaFilename
            else:
                return None
        elif (name in modelXbrl.nameConcepts):
            filename = None
            for _concept in modelXbrl.nameConcepts[name]:
                if (prefix == _concept.qname.prefix):
                    filename = _concept.modelDocument.uri
                    break
            if (not filename):
                return None
        else:
            return None
        return '{0}#{1}_{2}'.format(docRelpath(filename, thisDoc.thisLBdir), prefix, name)
    for thisDoc in genOrder:
        if (not thisDoc.generated):
            generateDoc(thisDoc, None, [])
    if priorCWD:
        os.chdir(priorCWD)
    return modelXbrl.modelDocument