Esempio n. 1
0
 def test_target_namespace_with_prefix_returns_target_namespace_and_prefix(
         self):
     xsd_string = "<schema targetNamespace='namespace' xmlns:ns='namespace'></schema>"
     xsd_tree = XSDTree.build_tree(xsd_string)
     namespaces = get_namespaces(xsd_string)
     self.assertEquals(('namespace', 'ns'),
                       get_target_namespace(xsd_tree, namespaces))
Esempio n. 2
0
def _get_target_namespace_prefix(ref_xml_schema_content, xml_doc_tree):
    """ Get the target namespace prefix.

    Args:
        ref_xml_schema_content:
        xml_doc_tree:

    Returns:

    """
    namespaces = get_namespaces(ref_xml_schema_content)
    target_namespace, target_ns_prefix = get_target_namespace(
        xml_doc_tree, namespaces)

    return target_ns_prefix
Esempio n. 3
0
def get_xpath_with_target_namespace(xpath, xsd_string):
    """Adds target namespace to a given XPath

    Params:
        xpath:
        xsd_string:

    Returns:

    """
    namespaces = get_target_namespace(XSDTree.build_tree(xsd_string),
                                      get_namespaces(xsd_string))

    xpath = xpath.format(namespaces[1])

    if namespaces[1] == "":
        xpath = xpath.replace(":", "")
        namespaces = None
    else:
        namespaces = {namespaces[1]: namespaces[0]}

    return xpath, namespaces
Esempio n. 4
0
 def test_target_namespace_no_prefix_returns_target_namespace_only(self):
     xsd_string = "<schema targetNamespace='namespace'></schema>"
     xsd_tree = XSDTree.build_tree(xsd_string)
     namespaces = get_namespaces(xsd_string)
     self.assertEqual(("namespace", ""),
                      get_target_namespace(xsd_tree, namespaces))
Esempio n. 5
0
 def test_no_target_namespace_returns_none_values(self):
     xsd_string = "<schema></schema>"
     xsd_tree = XSDTree.build_tree(xsd_string)
     namespaces = get_namespaces(xsd_string)
     self.assertEqual((None, ""),
                      get_target_namespace(xsd_tree, namespaces))
Esempio n. 6
0
def _insert_element_type(xsd_string, xpath, type_content, element_type_name,
                         include_url):
    """Insert an element of given type in xsd string.

    Args:
        xsd_string: xsd string
        xpath: xpath where to insert the element
        type_content: string content of the type to insert
        element_type_name: name of the type
        include_url: url used to reference the type in schemaLocation

    Returns:

    """
    # build the dom tree of the schema being built
    xsd_tree = XSDTree.build_tree(xsd_string)
    # get namespaces information for the schema
    namespaces = get_namespaces(xsd_string)
    # get the default namespace
    default_prefix = get_default_prefix(namespaces)
    # get target namespace information
    target_namespace, target_namespace_prefix = get_target_namespace(
        xsd_tree, namespaces)
    # build xpath to element
    xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE)
    # build xsd tree
    type_xsd_tree = XSDTree.build_tree(type_content)
    # get namespaces information for the type
    type_namespaces = get_namespaces(type_content)
    # get target namespace information
    type_target_namespace, type_target_namespace_prefix = get_target_namespace(
        type_xsd_tree, type_namespaces)

    # 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"]

    # format type name to avoid forbidden xml characters
    element_type_name = _get_valid_xml_name(element_type_name)

    # variable that indicates if namespaces map needs to be updated
    update_ns_map = False

    # Schema without target namespace
    if target_namespace is None:
        # Type without target namespace
        if type_target_namespace is None:
            # create type name with namespace
            ns_type_name = type_name
            # create include element
            dependency_tag = "include"
            dependency_attrib = {"schemaLocation": include_url}
        # Type with target namespace
        else:
            # create type name with namespace
            ns_type_name = _get_ns_type_name(type_target_namespace_prefix,
                                             type_name,
                                             prefix_required=True)
            # create import element
            dependency_tag = "import"
            dependency_attrib = {
                "schemaLocation": include_url,
                "namespace": type_target_namespace,
            }
            update_ns_map = True

    # Schema with target namespace
    else:
        # Type without target namespace
        if type_target_namespace is None:
            # create type name with namespace
            ns_type_name = _get_ns_type_name(target_namespace_prefix,
                                             type_name)
            # create include element
            dependency_tag = "include"
            dependency_attrib = {"schemaLocation": include_url}
        # Type with target namespace
        else:
            # Same target namespace as base template
            if target_namespace == type_target_namespace:
                # create type name with namespace
                ns_type_name = _get_ns_type_name(target_namespace_prefix,
                                                 type_name)
                # create include element
                dependency_tag = "include"
                dependency_attrib = {"schemaLocation": include_url}
            # Different target namespace as base template
            else:
                # create type name with namespace
                ns_type_name = _get_ns_type_name(type_target_namespace_prefix,
                                                 type_name,
                                                 prefix_required=True)
                # create import element
                dependency_tag = "import"
                dependency_attrib = {
                    "schemaLocation": include_url,
                    "namespace": type_target_namespace,
                }
                update_ns_map = True

    # create dependency element
    dependency_element = _create_xsd_element(dependency_tag, dependency_attrib)
    # create xsd element
    xsd_element = _create_xsd_element("element",
                                      attrib={
                                          "name": element_type_name,
                                          "type": ns_type_name
                                      })
    # check if dependency element (include/import) is already present
    dependency_tag = "{0}[@schemaLocation='{1}']".format(
        dependency_element.tag, dependency_element.attrib["schemaLocation"])
    dependency_present = xsd_tree.find(dependency_tag) is not None

    if not dependency_present:
        # add dependency element (include/import)
        xsd_tree.getroot().insert(0, dependency_element)

    # add xsd element
    xsd_tree.find(xpath).append(xsd_element)

    # if namespace map of the schema needs to be updated
    if not dependency_present and update_ns_map:
        root = xsd_tree.getroot()
        root_ns_map = root.nsmap

        if (type_target_namespace_prefix in list(root_ns_map.keys())
                and root_ns_map[type_target_namespace_prefix] !=
                type_target_namespace):
            raise CoreError(
                "The namespace prefix is already declared for a different namespace."
            )
        else:
            root_ns_map[type_target_namespace_prefix] = type_target_namespace
            new_root = XSDTree.create_element(root.tag,
                                              nsmap=root_ns_map,
                                              attrib=root.attrib)
            new_root[:] = root[:]

            # return result tree
            return new_root

    else:
        # return result tree
        return xsd_tree