diff --git a/.gitignore b/.gitignore index dfcfd56..1623ab0 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,4 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ +.idea/ \ No newline at end of file diff --git a/ApiManagementSchemaImport/Microsoft.Azure.ApiManagement.WsdlProcessor.Common/WsdlDocument.cs b/ApiManagementSchemaImport/Microsoft.Azure.ApiManagement.WsdlProcessor.Common/WsdlDocument.cs index 1c6e39c..79c9325 100644 --- a/ApiManagementSchemaImport/Microsoft.Azure.ApiManagement.WsdlProcessor.Common/WsdlDocument.cs +++ b/ApiManagementSchemaImport/Microsoft.Azure.ApiManagement.WsdlProcessor.Common/WsdlDocument.cs @@ -518,41 +518,49 @@ private static void AddXmlnsAndChangePrefixReferenced( IEnumerable newElements, Dictionary namespaces) { + // Capture all declared xmlns attributes var parentNamespaces = documentElement - .Attributes() - .Where(a => a.ToString() - .Contains("xmlns:")) - .Select(a => new - { - Prefix = a.Name.LocalName, - Namespace = a.Value - }).ToDictionary(a => a.Namespace, a => a.Prefix); + .Attributes() + .Where(a => a.IsNamespaceDeclaration) + .Select(a => new { Prefix = a.Name.LocalName, Namespace = a.Value }) + .ToDictionary(a => a.Namespace + "_" + a.Prefix, a => a.Prefix); // composite key foreach (var item in namespaces) { - //string prefix = string.Empty; - parentNamespaces.TryGetValue(item.Key, out string prefix); - - if (prefix == null) + string desiredPrefix = item.Value; + string prefix = null; + var attr = documentElement.Attribute(XNamespace.Xmlns + desiredPrefix); + if (attr != null) { - prefix = GenerateNewNamespace(documentElement, parentNamespaces, item); + if (attr.Value == item.Key) + { + prefix = desiredPrefix; + } + else + { + prefix = GenerateNewNamespace(documentElement, parentNamespaces, item); + } + } + else + { + documentElement.Add(new XAttribute(XNamespace.Xmlns + desiredPrefix, item.Key)); + parentNamespaces.Add(item.Key + "_" + desiredPrefix, desiredPrefix); + prefix = desiredPrefix; } - //Modify attributes from elements with the same prefix + // Modify attributes from elements that reference the old prefix var prefixValue = item.Value + ":"; - foreach (var element in newElements.DescendantsAndSelf()) { - //Go through all attributes and modify if they have the prefix + // Go through all attributes and modify if they have the prefix var attributes = element - .Attributes() - .Where(e => e.Value.Contains(prefixValue)); - + .Attributes() + .Where(e => e.Value.Contains(prefixValue)); foreach (var attribute in attributes) { if (attribute.Value.Count(i => i == ':') == 1 - && !(Uri.TryCreate(attribute.Value, UriKind.Absolute, out var uriResult) - && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))) + && !(Uri.TryCreate(attribute.Value, UriKind.Absolute, out var uriResult) + && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))) { var splitValue = attribute.Value.Split(':'); attribute.Value = prefix + ":" + splitValue[1]; diff --git a/examples/includeExample/additional.xsd b/examples/includeExample/additional.xsd new file mode 100644 index 0000000..a324428 --- /dev/null +++ b/examples/includeExample/additional.xsd @@ -0,0 +1,5 @@ + + + diff --git a/examples/includeExample/common.xsd b/examples/includeExample/common.xsd new file mode 100644 index 0000000..83aace1 --- /dev/null +++ b/examples/includeExample/common.xsd @@ -0,0 +1,5 @@ + + + diff --git a/examples/includeExample/example.wsdl b/examples/includeExample/example.wsdl new file mode 100644 index 0000000..ae42a9e --- /dev/null +++ b/examples/includeExample/example.wsdl @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/includeExample/schema1.xsd b/examples/includeExample/schema1.xsd new file mode 100644 index 0000000..a8caceb --- /dev/null +++ b/examples/includeExample/schema1.xsd @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/examples/includeExample/schema2.xsd b/examples/includeExample/schema2.xsd new file mode 100644 index 0000000..3b871c4 --- /dev/null +++ b/examples/includeExample/schema2.xsd @@ -0,0 +1,12 @@ + + + + + + + + + +