Browse Source

2005-01-11 Atsushi Enomoto <[email protected]>

	* XmlSchema.cs : Write() should handle carefully output
	  namespace/prefix mapping.

	* XmlSchemaTests.cs : added TestWriteNamespaces2 ().



svn path=/trunk/mcs/; revision=38718
Atsushi Eno 21 years ago
parent
commit
fbb0b0cdc3

+ 5 - 0
mcs/class/System.XML/System.Xml.Schema/ChangeLog

@@ -1,3 +1,8 @@
+2005-01-11  Atsushi Enomoto <[email protected]>
+
+	* XmlSchema.cs : Write() should handle carefully output
+	  namespace/prefix mapping.
+
 2004-12-26  Atsushi Enomoto <[email protected]>
 
 	* XmlSchemaSet.cs : added Remove(), RemoveRecursive(), Reprocess().

+ 13 - 4
mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs

@@ -871,8 +871,6 @@ namespace System.Xml.Schema
 			XmlSerializerNamespaces nss = new XmlSerializerNamespaces ();
 
 			if (namespaceManager != null) {
-				if (nss == null)
-					nss = new XmlSerializerNamespaces ();
 				foreach (string name in namespaceManager) {
 					//xml and xmlns namespaces are added by default in namespaceManager.
 					//So we should ignore them
@@ -882,10 +880,21 @@ namespace System.Xml.Schema
 			}
 
 			if (Namespaces != null && Namespaces.Count > 0) {
-				nss.Add (String.Empty, XmlSchema.Namespace);
-				foreach (XmlQualifiedName qn in Namespaces.ToArray ()) {
+				XmlQualifiedName [] qnames = Namespaces.ToArray ();
+				foreach (XmlQualifiedName qn in qnames)
 					nss.Add (qn.Name, qn.Namespace);
+				string p = String.Empty;
+				bool loop = true;
+				for (int idx = 1; loop; idx++) {
+					loop = false;
+					foreach (XmlQualifiedName qn in qnames)
+						if (qn.Name == p) {
+							p = "q" + idx;
+							loop = true;
+							break;
+						}
 				}
+				nss.Add (p, XmlSchema.Namespace);
 			}
 
 			if (nss.Count == 0) {

+ 4 - 0
mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog

@@ -1,3 +1,7 @@
+2004-01-11  Atsushi Enomoto <[email protected]>
+
+	* XmlSchemaTests.cs : added TestWriteNamespaces2 ().
+
 2004-09-13  Atsushi Enomoto <[email protected]>
 
 	* XmlSchemaSetTests.cs : added.

+ 34 - 0
mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaTests.cs

@@ -11,6 +11,7 @@ using System;
 using System.IO;
 using System.Xml;
 using System.Xml.Schema;
+using System.Xml.Serialization;
 using NUnit.Framework;
 
 namespace MonoTests.System.Xml
@@ -251,5 +252,38 @@ namespace MonoTests.System.Xml
 			doc.LoadXml (sw.ToString ());
 			AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
 		}
+
+		[Test]
+		public void TestWriteNamespaces2 ()
+		{
+			string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
+			XmlSchema xs = new XmlSchema ();
+			XmlSerializerNamespaces nss =
+				new XmlSerializerNamespaces ();
+			StringWriter sw;
+			sw = new StringWriter ();
+			xs.Write (new XmlTextWriter (sw));
+			AssertEquals (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
+
+			xs.Namespaces = nss;
+			sw = new StringWriter ();
+			xs.Write (new XmlTextWriter (sw));
+			AssertEquals (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
+
+			nss.Add ("foo", "urn:foo");
+			sw = new StringWriter ();
+			xs.Write (new XmlTextWriter (sw));
+			AssertEquals (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
+
+			nss.Add ("", "urn:foo");
+			sw = new StringWriter ();
+			xs.Write (new XmlTextWriter (sw));
+			AssertEquals (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
+
+			nss.Add ("q1", "urn:q1");
+			sw = new StringWriter ();
+			xs.Write (new XmlTextWriter (sw));
+			AssertEquals (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
+		}
 	}
 }