コード例 #1
0
def update_dependencies(xsd_tree, dependencies):
    # get the imports
    xsd_imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE))
    # get the includes
    xsd_includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE))

    for schema_location, dependency_id in dependencies.iteritems():
        if dependency_id != 'None':
            for xsd_include in xsd_includes:
                if schema_location == xsd_include.attrib['schemaLocation']:
                    xsd_include.attrib['schemaLocation'] = getSchemaLocation(dependency_id)

            for xsd_import in xsd_imports:
                if schema_location == xsd_import.attrib['schemaLocation']:
                    xsd_import.attrib['schemaLocation'] = getSchemaLocation(dependency_id)
コード例 #2
0
def update_dependencies(xsd_tree, dependencies):
    # get the imports
    xsd_imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE))
    # get the includes
    xsd_includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE))

    for schema_location, dependency_id in dependencies.iteritems():
        if dependency_id != 'None':
            for xsd_include in xsd_includes:
                if schema_location == xsd_include.attrib['schemaLocation']:
                    xsd_include.attrib['schemaLocation'] = getSchemaLocation(dependency_id)

            for xsd_import in xsd_imports:
                if schema_location == xsd_import.attrib['schemaLocation']:
                    xsd_import.attrib['schemaLocation'] = getSchemaLocation(dependency_id)
コード例 #3
0
def insert_element_sequence(request):
    type_id = request.POST['typeID']
    type_name = request.POST['typeName']
    xpath = request.POST['xpath']

    defaultPrefix = request.session['defaultPrefixCompose']
    namespace = LXML_SCHEMA_NAMESPACE

    xmlString = request.session['newXmlTemplateCompose']
    dom = etree.parse(BytesIO(xmlString.encode('utf-8')))

    # get the type to add
    includedType = Type.objects.get(pk=type_id)
    typeTree = etree.XML(str(includedType.content))
    elementType = typeTree.find("{}complexType".format(LXML_SCHEMA_NAMESPACE))
    if elementType is None:
        elementType = typeTree.find("{}simpleType".format(LXML_SCHEMA_NAMESPACE))
    type = elementType.attrib["name"]

    # set the element namespace
    xpath = xpath.replace(defaultPrefix + ":", namespace)
    # add the element to the sequence
    dom.find(xpath).append(etree.Element(namespace + "element", attrib={'type': type, 'name': type_name}))

    includeURL = getSchemaLocation(str(type_id))
    # add the id of the type if not already present
    if includeURL not in request.session['includedTypesCompose']:
        request.session['includedTypesCompose'].append(includeURL)
        dom.getroot().insert(0, etree.Element(namespace + "include", attrib={'schemaLocation': includeURL}))

    # save the tree in the session
    request.session['newXmlTemplateCompose'] = etree.tostring(dom)

    return HttpResponse(json.dumps({}), content_type='application/javascript')
コード例 #4
0
ファイル: ajax.py プロジェクト: hzhao1230/nanomine
def insert_element_sequence(request):
    type_id = request.POST['typeID']
    type_name = request.POST['typeName']
    xpath = request.POST['xpath']
    
    defaultPrefix = request.session['defaultPrefixCompose']
    namespace = request.session['namespacesCompose'][defaultPrefix]
    
    xmlString = request.session['newXmlTemplateCompose']
    dom = etree.parse(BytesIO(xmlString.encode('utf-8')))
    
    # get the type to add
    includedType = Type.objects.get(pk=type_id)
    typeTree = etree.XML(str(includedType.content))
    elementType = typeTree.find("{http://www.w3.org/2001/XMLSchema}complexType")
    if elementType is None:
        elementType = typeTree.find("{http://www.w3.org/2001/XMLSchema}simpleType")
    type = elementType.attrib["name"]
    
    # set the element namespace
    xpath = xpath.replace(defaultPrefix +":", namespace)
    # add the element to the sequence
    dom.find(xpath).append(etree.Element(namespace+"element", attrib={'type': type, 'name':type_name}))
    
    includeURL = getSchemaLocation(request, str(type_id))
    # add the id of the type if not already present
    if includeURL not in request.session['includedTypesCompose']:
        request.session['includedTypesCompose'].append(includeURL)        
        dom.getroot().insert(0,etree.Element(namespace+"include", attrib={'schemaLocation':includeURL}))
    
    # save the tree in the session
    request.session['newXmlTemplateCompose'] = etree.tostring(dom) 
    print etree.tostring(dom)
    
    return HttpResponse(json.dumps({}), content_type='application/javascript')
コード例 #5
0
ファイル: ajax.py プロジェクト: ramaranjanruj/MDCS
def insertElementSequence(request, typeID, xpath, typeName):
    dajax = Dajax()

    defaultPrefix = request.session["defaultPrefixCompose"]
    namespace = request.session["namespacesCompose"][defaultPrefix]

    xmlString = request.session["newXmlTemplateCompose"]
    dom = etree.parse(BytesIO(xmlString.encode("utf-8")))

    # get the type to add
    includedType = Type.objects.get(pk=typeID)
    typeTree = etree.fromstring(includedType.content)
    elementType = typeTree.find("{http://www.w3.org/2001/XMLSchema}complexType")
    if elementType is None:
        elementType = typeTree.find("{http://www.w3.org/2001/XMLSchema}simpleType")
    type = elementType.attrib["name"]

    # set the element namespace
    xpath = xpath.replace(defaultPrefix + ":", namespace)
    # add the element to the sequence
    dom.find(xpath).append(etree.Element(namespace + "element", attrib={"type": type, "name": typeName}))

    includeURL = getSchemaLocation(request, str(typeID))
    # add the id of the type if not already present
    if includeURL not in request.session["includedTypesCompose"]:
        request.session["includedTypesCompose"].append(includeURL)
        dom.getroot().insert(0, etree.Element(namespace + "include", attrib={"schemaLocation": includeURL}))

    # save the tree in the session
    request.session["newXmlTemplateCompose"] = etree.tostring(dom)
    print etree.tostring(dom)

    return dajax.json()
コード例 #6
0
ファイル: common.py プロジェクト: Huchikoma/internship_MMQ
def update_dependencies(xsd_tree, dependencies):
    """
    Replace local schemaLocation by some accessible from MDCS
    :param xsd_tree:
    :param dependencies:
    :return:
    """
    # get the imports
    xsd_imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE))
    # get the includes
    xsd_includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE))

    for schema_location, dependency_id in dependencies.iteritems():
        if dependency_id != 'None':
            for xsd_include in xsd_includes:
                if schema_location == xsd_include.attrib['schemaLocation']:
                    xsd_include.attrib['schemaLocation'] = getSchemaLocation(dependency_id)

            for xsd_import in xsd_imports:
                if schema_location == xsd_import.attrib['schemaLocation']:
                    xsd_import.attrib['schemaLocation'] = getSchemaLocation(dependency_id)
コード例 #7
0
def insertElementSequence(request, typeID, xpath, typeName):
    dajax = Dajax()

    defaultPrefix = request.session['defaultPrefixCompose']
    namespace = request.session['namespacesCompose'][defaultPrefix]

    xmlString = request.session['newXmlTemplateCompose']
    dom = etree.parse(BytesIO(xmlString.encode('utf-8')))

    # get the type to add
    includedType = Type.objects.get(pk=typeID)
    typeTree = etree.fromstring(includedType.content)
    elementType = typeTree.find(
        "{http://www.w3.org/2001/XMLSchema}complexType")
    if elementType is None:
        elementType = typeTree.find(
            "{http://www.w3.org/2001/XMLSchema}simpleType")
    type = elementType.attrib["name"]

    # set the element namespace
    xpath = xpath.replace(defaultPrefix + ":", namespace)
    # add the element to the sequence
    dom.find(xpath).append(
        etree.Element(namespace + "element",
                      attrib={
                          'type': type,
                          'name': typeName
                      }))

    includeURL = getSchemaLocation(request, str(typeID))
    # add the id of the type if not already present
    if includeURL not in request.session['includedTypesCompose']:
        request.session['includedTypesCompose'].append(includeURL)
        dom.getroot().insert(
            0,
            etree.Element(namespace + "include",
                          attrib={'schemaLocation': includeURL}))

    # save the tree in the session
    request.session['newXmlTemplateCompose'] = etree.tostring(dom)
    print etree.tostring(dom)

    return dajax.json()
コード例 #8
0
ファイル: ajax.py プロジェクト: hzhao1230/nanomine
def resolve_dependencies(request):
    print 'BEGIN def resolveDependencies(request)'
    dependencies = request.POST.getlist('dependencies[]')
     
    objectContent = None
    
    response_dict = {}
    if ('uploadObjectName' in request.session and request.session['uploadObjectName'] is not None and
        'uploadObjectFilename' in request.session and request.session['uploadObjectFilename'] is not None and
        'uploadObjectContent' in request.session and request.session['uploadObjectContent'] is not None and
        'uploadObjectType' in request.session and request.session['uploadObjectType'] is not None):    
        objectContent = request.session['uploadObjectContent']
#         contentSession = 'uploadObjectContent'
        validSession = 'uploadObjectValid'
        flatSession = 'uploadObjectFlat'
        apiSession = 'uploadObjectAPIurl'
        saveBtn = "<span class='btn' onclick='saveObject()'>Save</span>"
    elif ('uploadVersionFilename' in request.session and request.session['uploadVersionFilename'] is not None and
        'uploadVersionContent' in request.session and request.session['uploadVersionContent'] is not None):
        objectContent = request.session['uploadVersionContent']
#         contentSession = 'uploadVersionContent'
        validSession = 'uploadVersionValid'
        flatSession = 'uploadVersionFlat'
        apiSession = 'uploadVersionAPIurl'
        saveBtn = "<span class='btn' onclick='saveVersion()'>Save</span>"
    else:
        response_dict= {'errors': "Please upload a file first."}
        return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
         
    xmlTree = etree.parse(BytesIO(objectContent.encode('utf-8')))        
    # get the imports
#     imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import")
     
    # get the includes
    includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include")
     
    idxInclude = 0        
    # replace includes/imports by API calls
    for dependency in dependencies:
        includes[idxInclude].attrib['schemaLocation'] = getSchemaLocation(request, str(dependency))
        idxInclude += 1            
     
#         flattener = XSDFlattenerURL(etree.tostring(xmlTree),'admin','admin')
    flattener = XSDFlattenerMDCS(etree.tostring(xmlTree))
    flatStr = flattener.get_flat()
    flatTree = etree.fromstring(flatStr)
    
    try:
        # is it a valid XML schema ?
        xmlSchema = etree.XMLSchema(flatTree)
#         request.session[contentSession] = etree.tostring(xmlTree)
        request.session[validSession] = True
        
        request.session[flatSession] = flatStr
        request.session[apiSession] = etree.tostring(xmlTree)
        request.session["uploadDependencies"] = dependencies
        message = "The uploaded template is valid. You can now save it." + saveBtn
        response_dict = {'message': message}
    except Exception, e:
        response_dict = {'errorDependencies': e.message.replace("'", "")}
        return HttpResponse(json.dumps(response_dict), content_type='application/javascript')      
コード例 #9
0
def resolve_dependencies(request):
    print 'BEGIN def resolveDependencies(request)'
    dependencies = request.POST.getlist('dependencies[]')

    objectContent = None

    response_dict = {}
    if ('uploadObjectName' in request.session
            and request.session['uploadObjectName'] is not None
            and 'uploadObjectFilename' in request.session
            and request.session['uploadObjectFilename'] is not None
            and 'uploadObjectContent' in request.session
            and request.session['uploadObjectContent'] is not None
            and 'uploadObjectType' in request.session
            and request.session['uploadObjectType'] is not None):
        objectContent = request.session['uploadObjectContent']
        #         contentSession = 'uploadObjectContent'
        validSession = 'uploadObjectValid'
        flatSession = 'uploadObjectFlat'
        apiSession = 'uploadObjectAPIurl'
        saveBtn = "<span class='btn' onclick='saveObject()'>Save</span>"
    elif ('uploadVersionFilename' in request.session
          and request.session['uploadVersionFilename'] is not None
          and 'uploadVersionContent' in request.session
          and request.session['uploadVersionContent'] is not None):
        objectContent = request.session['uploadVersionContent']
        #         contentSession = 'uploadVersionContent'
        validSession = 'uploadVersionValid'
        flatSession = 'uploadVersionFlat'
        apiSession = 'uploadVersionAPIurl'
        saveBtn = "<span class='btn' onclick='saveVersion()'>Save</span>"
    else:
        response_dict = {'errors': "Please upload a file first."}
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')

    xmlTree = etree.parse(BytesIO(objectContent.encode('utf-8')))
    # get the imports
    #     imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import")

    # get the includes
    includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include")

    idxInclude = 0
    # replace includes/imports by API calls
    for dependency in dependencies:
        includes[idxInclude].attrib['schemaLocation'] = getSchemaLocation(
            request, str(dependency))
        idxInclude += 1

#         flattener = XSDFlattenerURL(etree.tostring(xmlTree),'admin','admin')
    flattener = XSDFlattenerMDCS(etree.tostring(xmlTree))
    flatStr = flattener.get_flat()
    flatTree = etree.fromstring(flatStr)

    try:
        # is it a valid XML schema ?
        xmlSchema = etree.XMLSchema(flatTree)
        #         request.session[contentSession] = etree.tostring(xmlTree)
        request.session[validSession] = True

        request.session[flatSession] = flatStr
        request.session[apiSession] = etree.tostring(xmlTree)
        request.session["uploadDependencies"] = dependencies
        message = "The uploaded template is valid. You can now save it." + saveBtn
        response_dict = {'message': message}
    except Exception, e:
        response_dict = {'errorDependencies': e.message.replace("'", "")}
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')
コード例 #10
0
ファイル: ajax.py プロジェクト: RayPlante/RDA
def insert_element_sequence(request):
    try:
        type_id = request.POST['typeID']
        client_type_name = request.POST['typeName']
        xpath = request.POST['xpath']

        xml_tree_str = request.session['newXmlTemplateCompose']

        # build the dom tree of the schema being built
        xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8')))
        # get namespaces information for the schema
        namespaces = get_namespaces(BytesIO(str(xml_tree_str)))
        default_prefix = get_default_prefix(namespaces)
        # get target namespace information
        target_namespace, target_namespace_prefix = get_target_namespace(
            namespaces, xsd_tree)
        # build xpath to element
        xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE)
        if type_id == 'built_in_type':
            type_name = default_prefix + ':' + client_type_name
            xsd_tree.find(xpath).append(
                etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                              attrib={
                                  'type': type_name,
                                  'name': client_type_name
                              }))
            # validate XML schema
            error = validate_xml_schema(xsd_tree)
            new_xsd_str = etree.tostring(xsd_tree)
        else:
            # get the type being included
            type_object = Type.objects().get(pk=type_id)
            type_xsd_tree = etree.parse(
                BytesIO(type_object.content.encode('utf-8')))
            # get namespaces information for the type
            type_namespaces = get_namespaces(BytesIO(str(type_object.content)))
            type_target_namespace, type_target_namespace_prefix = get_target_namespace(
                type_namespaces, type_xsd_tree)

            # get the type from the included/imported file
            # If there is a complex type
            element_type = type_xsd_tree.find(
                "{}complexType".format(LXML_SCHEMA_NAMESPACE))
            if element_type is None:
                # If there is a simple type
                element_type = type_xsd_tree.find(
                    "{}simpleType".format(LXML_SCHEMA_NAMESPACE))
            type_name = element_type.attrib["name"]

            if type_target_namespace is not None:
                ns_type_name = "{0}:{1}".format(type_target_namespace_prefix,
                                                type_name)
            else:
                if target_namespace is not None:
                    ns_type_name = "{0}:{1}".format(target_namespace_prefix,
                                                    type_name)
                else:
                    ns_type_name = '{}'.format(type_name)
            nsmap = {type_target_namespace_prefix: type_target_namespace}
            update_nsmap = False

            # get link to the type to include
            include_url = getSchemaLocation(str(type_id))

            # Schema without target namespace
            if target_namespace is None:
                # Type without target namespace
                if type_target_namespace is None:
                    # add include
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element(
                            "{}include".format(LXML_SCHEMA_NAMESPACE),
                            attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(
                        etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'type': type_name,
                                'name': client_type_name
                            }))
                # Type with target namespace
                else:
                    # add import
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                      attrib={
                                          'schemaLocation': include_url,
                                          'namespace': type_target_namespace
                                      }))
                    # create the element to add
                    element = etree.Element(
                        "{}element".format(LXML_SCHEMA_NAMESPACE),
                        attrib={
                            'name': client_type_name,
                            'type': ns_type_name
                        },
                    )
                    # add the element
                    xsd_tree.find(xpath).append(element)

                    update_nsmap = True

            # Schema with target namespace
            else:
                # Type without target namespace
                if type_target_namespace is None:
                    # add include
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element(
                            "{}include".format(LXML_SCHEMA_NAMESPACE),
                            attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(
                        etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'name': client_type_name,
                                'type': ns_type_name
                            }))
                # Type with target namespace
                else:
                    # Same target namespace as base template
                    if target_namespace == type_target_namespace:
                        # add include
                        xsd_tree.getroot().insert(
                            0,
                            etree.Element(
                                "{}include".format(LXML_SCHEMA_NAMESPACE),
                                attrib={'schemaLocation': include_url}))
                        # add element
                        xsd_tree.find(xpath).append(
                            etree.Element(
                                "{}element".format(LXML_SCHEMA_NAMESPACE),
                                attrib={
                                    'name': client_type_name,
                                    'type': ns_type_name
                                }))
                    # Different target namespace as base template
                    else:
                        # add import
                        xsd_tree.getroot().insert(
                            0,
                            etree.Element(
                                "{}import".format(LXML_SCHEMA_NAMESPACE),
                                attrib={
                                    'schemaLocation': include_url,
                                    'namespace': type_target_namespace
                                }))
                        # create the element to add
                        element = etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'name': client_type_name,
                                'type': ns_type_name
                            },
                        )
                        # add the element
                        xsd_tree.find(xpath).append(element)

                        update_nsmap = True

            # add the id of the type if not already present
            if include_url not in request.session['includedTypesCompose']:
                request.session['includedTypesCompose'].append(include_url)

            if update_nsmap:
                root = xsd_tree.getroot()
                root_nsmap = root.nsmap

                if type_target_namespace_prefix in root_nsmap.keys() and\
                                root_nsmap[type_target_namespace_prefix] != type_target_namespace:
                    raise MDCSError(
                        'The namespace prefix is already declared for a different namespace.'
                    )
                else:
                    root_nsmap[
                        type_target_namespace_prefix] = type_target_namespace
                    new_root = etree.Element(root.tag, nsmap=root_nsmap)
                    new_root[:] = root[:]

                    # validate XML schema
                    error = validate_xml_schema(new_root)
                    new_xsd_str = etree.tostring(new_root)

            else:
                # validate XML schema
                error = validate_xml_schema(xsd_tree)
                new_xsd_str = etree.tostring(xsd_tree)

        if error is not None:
            raise MDCSError(error)

        # save the tree in the session
        request.session['newXmlTemplateCompose'] = new_xsd_str
    except Exception, e:
        return HttpResponseBadRequest(e.message,
                                      content_type='application/javascript')
コード例 #11
0
def insert_element_sequence(request):
    try:
        type_id = request.POST['typeID']
        client_type_name = request.POST['typeName']
        xpath = request.POST['xpath']

        xml_tree_str = request.session['newXmlTemplateCompose']

        # build the dom tree of the schema being built
        xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8')))
        # get namespaces information for the schema
        namespaces = get_namespaces(BytesIO(str(xml_tree_str)))
        default_prefix = get_default_prefix(namespaces)
        target_namespace, target_namespace_prefix = get_target_namespace(namespaces, xsd_tree)

        # get the type being included
        type_object = Type.objects().get(pk=type_id)
        type_xsd_tree = etree.parse(BytesIO(type_object.content.encode('utf-8')))
        # get namespaces information for the type
        type_namespaces = get_namespaces(BytesIO(str(type_object.content)))
        type_target_namespace, type_target_namespace_prefix = get_target_namespace(type_namespaces, type_xsd_tree)

        # get the type from the included/imported file
        # If there is a complex type
        element_type = type_xsd_tree.find("{}complexType".format(LXML_SCHEMA_NAMESPACE))
        if element_type is None:
            # If there is a simple type
            element_type = type_xsd_tree.find("{}simpleType".format(LXML_SCHEMA_NAMESPACE))
        type_name = element_type.attrib["name"]

        if type_target_namespace is not None:
            ns_type_name = "{0}:{1}".format(type_target_namespace_prefix, type_name)
        else:
            if target_namespace is not None:
                ns_type_name = "{0}:{1}".format(target_namespace_prefix, type_name)
            else:
                ns_type_name = '{}'.format(type_name)
        nsmap = {type_target_namespace_prefix: type_target_namespace}
        update_nsmap = False

        # build xpath to element
        xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE)

        # get link to the type to include
        include_url = getSchemaLocation(str(type_id))

        # Schema without target namespace
        if target_namespace is None:
            # Type without target namespace
            if type_target_namespace is None:
                # add include
                xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url}))
                # add element
                xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'type': type_name,
                                                    'name': client_type_name}))
            # Type with target namespace
            else:
                # add import
                xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url,
                                                  'namespace': type_target_namespace}))
                # create the element to add
                element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                        attrib={'name': client_type_name,
                                                'type': ns_type_name},
                                        )
                # add the element
                xsd_tree.find(xpath).append(element)

                update_nsmap = True

        # Schema with target namespace
        else:
            # Type without target namespace
            if type_target_namespace is None:
                # add include
                xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url}))
                # add element
                xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'name': client_type_name,
                                                    'type': ns_type_name}))
            # Type with target namespace
            else:
                # Same target namespace as base template
                if target_namespace == type_target_namespace:
                    # add include
                    xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                              attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                                attrib={'name': client_type_name,
                                                        'type': ns_type_name}))
                # Different target namespace as base template
                else:
                    # add import
                    xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                              attrib={'schemaLocation': include_url,
                                                      'namespace': type_target_namespace}))
                    # create the element to add
                    element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'name': client_type_name,
                                                    'type': ns_type_name},
                                            )
                    # add the element
                    xsd_tree.find(xpath).append(element)

                    update_nsmap = True

        # add the id of the type if not already present
        if include_url not in request.session['includedTypesCompose']:
            request.session['includedTypesCompose'].append(include_url)

        if update_nsmap:
            root = xsd_tree.getroot()
            root_nsmap = root.nsmap

            if type_target_namespace_prefix in root_nsmap.keys() and\
                            root_nsmap[type_target_namespace_prefix] != type_target_namespace:
                raise MDCSError('The namespace prefix is already declared for a different namespace.')
            else:
                root_nsmap[type_target_namespace_prefix] = type_target_namespace
                new_root = etree.Element(root.tag, nsmap=root_nsmap)
                new_root[:] = root[:]

                # validate XML schema
                error = validate_xml_schema(new_root)

                new_xsd_str = etree.tostring(new_root)

        else:
            # validate XML schema
            error = validate_xml_schema(xsd_tree)
            new_xsd_str = etree.tostring(xsd_tree)

        if error is not None:
            raise MDCSError(error)

        # save the tree in the session
        request.session['newXmlTemplateCompose'] = new_xsd_str
    except Exception, e:
        return HttpResponseBadRequest(e.message, content_type='application/javascript')