2005-01-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexType.cs
index e03f3dc57641aba62eb9cab67610a797fb0c1543..9baea7d906ccbd4e9cc8a79aa2df911a1b7be268 100755 (executable)
@@ -5,6 +5,27 @@
 //     Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
 //     Enomoto, Atsushi     ginga@kit.hi-ho.ne.jp\r
 //\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;\r
 using System.Collections;\r
 using System.Xml;\r
@@ -25,6 +46,7 @@ namespace System.Xml.Schema
                private XmlSchemaDerivationMethod block;\r
                private XmlSchemaDerivationMethod blockResolved;\r
                private XmlSchemaContentModel contentModel;\r
+               private XmlSchemaParticle validatableParticle;\r
                private XmlSchemaParticle contentTypeParticle;\r
                private bool isAbstract;\r
                private bool isMixed;\r
@@ -34,7 +56,7 @@ namespace System.Xml.Schema
                internal bool ValidatedIsAbstract;\r
                internal bool ParentIsSchema = false;\r
 \r
-               private static string xmlname = "complexType";\r
+               const string xmlname = "complexType";\r
 \r
                private static XmlSchemaComplexType anyType;\r
 \r
@@ -44,10 +66,15 @@ namespace System.Xml.Schema
                                        anyType = new XmlSchemaComplexType ();\r
                                        anyType.Name = "";      // In MS.NET, it is not "anyType"\r
                                        anyType.QNameInternal = XmlQualifiedName.Empty; // Not xs:anyType as well.\r
-                                       anyType.contentTypeParticle = XmlSchemaAny.AnyTypeContent;\r
-//                                     anyType.baseSchemaTypeInternal = anyType;\r
-                                       anyType.datatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
+#if BUGGY_MS_COMPLIANT\r
+                                       anyType.validatableParticle = XmlSchemaParticle.Empty; // This code makes validator handles these schemas incorrectly: particlesIb001, mgM013, mgH014, ctE004, ctD004\r
+#else\r
+                                       anyType.validatableParticle = XmlSchemaAny.AnyTypeContent;\r
+#endif\r
+                                       anyType.contentTypeParticle = anyType.validatableParticle;\r
+                                       anyType.DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
                                        anyType.isMixed = true;\r
+                                       anyType.resolvedContentType = XmlSchemaContentType.Mixed;\r
                                }\r
                                return anyType;\r
                        }\r
@@ -55,11 +82,13 @@ namespace System.Xml.Schema
 \r
                internal static readonly XmlQualifiedName AnyTypeName = new XmlQualifiedName ("anyType", XmlSchema.Namespace);\r
 \r
-               public XmlSchemaComplexType()\r
+               public XmlSchemaComplexType ()\r
                {\r
                        attributes = new XmlSchemaObjectCollection();\r
                        block = XmlSchemaDerivationMethod.None;\r
                        attributeUses = new XmlSchemaObjectTable();\r
+                       validatableParticle = XmlSchemaParticle.Empty;\r
+                       contentTypeParticle = validatableParticle;\r
                }\r
 \r
                #region Attributes\r
@@ -131,7 +160,6 @@ namespace System.Xml.Schema
                        get{ return resolvedContentType; }\r
                }\r
                [XmlIgnore]\r
-               [MonoTODO ("Derivation is not supported yet.")]\r
                public XmlSchemaParticle ContentTypeParticle \r
                {\r
                        get{ return contentTypeParticle; }\r
@@ -151,8 +179,34 @@ namespace System.Xml.Schema
                {\r
                        get{ return attributeWildcard; }\r
                }\r
+\r
+#if NET_2_0\r
+               [XmlIgnore]\r
+               // LAMESPEC: This property is based on the premise that\r
+               // every particle has a unique name, but actually particles\r
+               // can be like:\r
+               // <sequence>\r
+               //  <element name='foo'/>\r
+               //  <element name='foo'>\r
+               //    <annotation>\r
+               //      <documentation>blah</documentation>\r
+               //    </annotation>\r
+               //  </element>\r
+               // </sequence>\r
+               //\r
+               // So with this signature this property can never be correct.\r
+               public XmlSchemaObjectTable LocalElements {\r
+                       get { throw new NotImplementedException (); }\r
+               }\r
+#endif\r
+\r
                #endregion\r
 \r
+               internal XmlSchemaParticle ValidatableParticle \r
+               {\r
+                       get{ return contentTypeParticle; }\r
+               }\r
+\r
                /// <remarks>\r
                /// 1. If ContentModel is present, neither particle nor Attributes nor AnyAttribute can be present.\r
                /// 2. If particle is present, \r
@@ -167,13 +221,23 @@ namespace System.Xml.Schema
                ///             4. block must be absent\r
                ///             \r
                /// </remarks>\r
-               [MonoTODO]\r
-               internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
+               internal override int Compile (ValidationEventHandler h, XmlSchema schema)\r
                {\r
                        // If this is already compiled this time, simply skip.\r
                        if (this.IsComplied (schema.CompilationId))\r
                                return errorCount;\r
 \r
+#if NET_2_0\r
+                       if (ContentModel != null)\r
+                               ContentModel.Parent = this;\r
+                       if (Particle != null)\r
+                               Particle.Parent = this;\r
+                       if (AnyAttribute != null)\r
+                               AnyAttribute.Parent = this;\r
+                       foreach (XmlSchemaObject obj in Attributes)\r
+                               obj.Parent = this;\r
+#endif\r
+\r
                        ValidatedIsAbstract = isAbstract;\r
 \r
                        if (isRedefinedComponent) {\r
@@ -207,7 +271,6 @@ namespace System.Xml.Schema
                                        }\r
                                        else\r
                                        {\r
-                                               //TODO: Check what all is not allowed\r
                                                if ((Block & XmlSchemaUtil.ComplexTypeBlockAllowed) != Block)\r
                                                        error (h, "Invalid block specification.");\r
                                                blockResolved = Block & XmlSchemaUtil.ComplexTypeBlockAllowed;\r
@@ -271,65 +334,25 @@ namespace System.Xml.Schema
                                        error(h,"attributes, particles or anyattribute is not allowed if ContentModel is present");\r
                                errorCount += contentModel.Compile (h, schema);\r
 \r
-                               XmlQualifiedName baseTypeName = null;\r
                                XmlSchemaSimpleContent smodel = ContentModel as XmlSchemaSimpleContent;\r
                                if(smodel != null)\r
                                {\r
                                        XmlSchemaSimpleContentExtension sscx = smodel.Content as XmlSchemaSimpleContentExtension;\r
-                                       if (sscx != null)\r
-                                               baseTypeName = sscx.BaseTypeName;\r
-                                       else {\r
+                                       if (sscx == null) {\r
                                                XmlSchemaSimpleContentRestriction sscr = smodel.Content as XmlSchemaSimpleContentRestriction;\r
                                                if (sscr != null) {\r
-                                                       baseTypeName = sscr.BaseTypeName;\r
                                                        if (sscr.BaseType != null) {\r
                                                                sscr.BaseType.Compile (h, schema);\r
-                                                               baseSchemaTypeInternal = sscr.BaseType;\r
+                                                               BaseXmlSchemaTypeInternal = sscr.BaseType;\r
                                                        }\r
                                                }\r
                                        }\r
-                                       contentTypeParticle = XmlSchemaParticle.Empty;\r
-                               }\r
-                               else\r
-                               {\r
-                                       XmlSchemaComplexContent cmodel = (XmlSchemaComplexContent) ContentModel;\r
-                                       XmlSchemaComplexContentExtension sccx = cmodel.Content as XmlSchemaComplexContentExtension;\r
-                                       if (sccx != null) {\r
-                                               contentTypeParticle = sccx.Particle;\r
-                                               baseTypeName = sccx.BaseTypeName;\r
-                                       }\r
-                                       else {\r
-                                               XmlSchemaComplexContentRestriction sccr = (XmlSchemaComplexContentRestriction) cmodel.Content;\r
-                                               if (sccr != null) {\r
-                                                       contentTypeParticle = sccr.Particle;\r
-                                                       baseTypeName = sccr.BaseTypeName;\r
-                                               }\r
-                                       }\r
                                }\r
                        }\r
                        else\r
                        {\r
-                               if(Particle is XmlSchemaGroupRef)\r
-                               {\r
-                                       XmlSchemaGroupRef xsgr = (XmlSchemaGroupRef)Particle;\r
-                                       errorCount += xsgr.Compile(h,schema);\r
-                               }\r
-                               else if(Particle is XmlSchemaAll)\r
-                               {\r
-                                       XmlSchemaAll xsa = (XmlSchemaAll)Particle;\r
-                                       errorCount += xsa.Compile(h,schema);\r
-                               }\r
-                               else if(Particle is XmlSchemaChoice)\r
-                               {\r
-                                       XmlSchemaChoice xsc = (XmlSchemaChoice)Particle;\r
-                                       errorCount += xsc.Compile(h,schema);\r
-                               }\r
-                               else if(Particle is XmlSchemaSequence)\r
-                               {\r
-                                       XmlSchemaSequence xss = (XmlSchemaSequence)Particle;\r
-                                       errorCount += xss.Compile(h,schema);\r
-                               }\r
-                               this.contentTypeParticle = Particle;\r
+                               if (Particle != null)\r
+                                       errorCount += Particle.Compile (h, schema);\r
 \r
                                if(this.anyAttribute != null)\r
                                {\r
@@ -357,8 +380,214 @@ namespace System.Xml.Schema
                        return errorCount;\r
                }\r
 \r
-               [MonoTODO]\r
-               internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
+               Guid CollectProcessId;\r
+\r
+               private void CollectSchemaComponent (ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       if (CollectProcessId == schema.CompilationId)\r
+                               return;\r
+                       // Below are already contributed by Compile():\r
+                       // {name}, {namespace} => QualifiedName, QNameInternal\r
+                       // {abstract} => ValidatedIsAbstract\r
+                       // {prohibited substitutions} => BlockResolved\r
+                       // {final} => FinalResolved\r
+                       // {annotations} => Annotation (XmlSchemaAnnotated)\r
+\r
+                       // Below are different properties depending on simpleContent | complexContent.\r
+                       // {base type definition}\r
+                       // {derivation method}\r
+                       // {attribute uses} => AttributeUses (later)\r
+                       // {attribute wildcard} => AttributeWildcard (later)\r
+                       // {content type}\r
+\r
+\r
+                       // {base type definition} => baseSchemaTypeInternal (later)\r
+                       if (contentModel != null) {\r
+                               BaseSchemaTypeName = contentModel.Content != null ? contentModel.Content.GetBaseTypeName () : XmlQualifiedName.Empty;\r
+\r
+                               BaseXmlSchemaTypeInternal = schema.SchemaTypes [BaseSchemaTypeName] as XmlSchemaType;\r
+                       }\r
+                       // Resolve redefine.\r
+                       if (this.isRedefineChild && BaseXmlSchemaType != null && this.QualifiedName == BaseSchemaTypeName) {\r
+                               XmlSchemaType redType = (XmlSchemaType) redefinedObject;\r
+                               if (redType == null)\r
+                                       error (h, "Redefinition base type was not found.");\r
+                               else\r
+                                       BaseXmlSchemaTypeInternal = redType;\r
+                       }\r
+\r
+                       // {derivation method} => resolvedDerivedBy\r
+                       if (contentModel != null && contentModel.Content != null) {\r
+                               resolvedDerivedBy =\r
+                                       contentModel.Content.IsExtension ?\r
+                                       XmlSchemaDerivationMethod.Extension :\r
+                                       XmlSchemaDerivationMethod.Restriction;\r
+                       }\r
+                       else\r
+                               resolvedDerivedBy = XmlSchemaDerivationMethod.Empty;\r
+\r
+\r
+                       // {content type} => ContentType and ContentTypeParticle (later)\r
+                       if (ContentModel != null) {\r
+                               CollectContentTypeFromContentModel (h, schema);\r
+                       } else\r
+                               CollectContentTypeFromImmediateContent ();\r
+                       contentTypeParticle = validatableParticle.GetOptimizedParticle (true);\r
+                       if (contentTypeParticle == XmlSchemaParticle.Empty && resolvedContentType == XmlSchemaContentType.ElementOnly)\r
+                               resolvedContentType = XmlSchemaContentType.Empty;\r
+\r
+                       CollectProcessId = schema.CompilationId;\r
+               }\r
+\r
+               #region {content type}\r
+               private void CollectContentTypeFromImmediateContent ()\r
+               {\r
+                       // leave resolvedDerivedBy as Empty\r
+                       if (Particle != null)\r
+                               validatableParticle = Particle;\r
+                       if (this == AnyType) {\r
+                               resolvedContentType = XmlSchemaContentType.Mixed;\r
+                               return;\r
+                       }\r
+\r
+                       if (validatableParticle == XmlSchemaParticle.Empty) {\r
+                               // note that this covers "Particle == null" case\r
+                               if (this.IsMixed)\r
+                                       resolvedContentType = XmlSchemaContentType.TextOnly;\r
+                               else\r
+                                       resolvedContentType = XmlSchemaContentType.Empty;\r
+                       } else {\r
+                               if (this.IsMixed)\r
+                                       resolvedContentType = XmlSchemaContentType.Mixed;\r
+                               else\r
+                                       resolvedContentType = XmlSchemaContentType.ElementOnly;\r
+                       }\r
+                       if (this != AnyType)\r
+                               BaseXmlSchemaTypeInternal = XmlSchemaComplexType.AnyType;\r
+               }\r
+\r
+               private void CollectContentTypeFromContentModel (ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       if (ContentModel.Content == null) {\r
+                               // basically it is error. Recover by specifying empty content.\r
+                               validatableParticle = XmlSchemaParticle.Empty;\r
+                               resolvedContentType = XmlSchemaContentType.Empty;\r
+                               return;\r
+                       }\r
+\r
+                       if (ContentModel.Content is XmlSchemaComplexContentExtension)\r
+                               CollectContentTypeFromComplexExtension (h, schema);\r
+                       if (ContentModel.Content is XmlSchemaComplexContentRestriction)\r
+                               CollectContentTypeFromComplexRestriction ();\r
+               }\r
+\r
+               private void CollectContentTypeFromComplexExtension (ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       XmlSchemaComplexContentExtension cce = (XmlSchemaComplexContentExtension) ContentModel.Content;\r
+                       XmlSchemaComplexType baseComplexType = this.BaseXmlSchemaType as XmlSchemaComplexType;\r
+                       if (baseComplexType != null)\r
+                               baseComplexType.CollectSchemaComponent (h ,schema);\r
+\r
+                       // It must exist, but consider validation error case.\r
+                       if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
+                               baseComplexType = XmlSchemaComplexType.AnyType;\r
+\r
+                       // On error case, it simple reject any contents\r
+                       if (baseComplexType == null) {\r
+                               validatableParticle = XmlSchemaParticle.Empty;\r
+                               resolvedContentType = XmlSchemaContentType.Empty;\r
+                               return;\r
+                       }\r
+\r
+                       // 3.4.2 complex content {content type}\r
+                       if (cce.Particle == null || cce.Particle == XmlSchemaParticle.Empty) {\r
+                               // - 2.1\r
+                               if (baseComplexType == null) {\r
+                                       // Basically it is an error. Considering ValidationEventHandler.\r
+                                       validatableParticle = XmlSchemaParticle.Empty;\r
+                                       resolvedContentType = XmlSchemaContentType.Empty;\r
+                               } else {\r
+                                       validatableParticle = baseComplexType.ValidatableParticle;\r
+                                       resolvedContentType = baseComplexType.resolvedContentType;\r
+                               }\r
+                       } else if (baseComplexType.validatableParticle == XmlSchemaParticle.Empty\r
+                               || baseComplexType == XmlSchemaComplexType.AnyType) {\r
+                               // - 2.2\r
+                               validatableParticle = cce.Particle;\r
+                               resolvedContentType = GetComplexContentType (contentModel);\r
+                       } else {\r
+                               // - 2.3 : create a new sequences that merges both contents.\r
+                               XmlSchemaSequence seq = new XmlSchemaSequence ();\r
+                               this.CopyInfo (seq);\r
+                               seq.Items.Add (baseComplexType.validatableParticle);\r
+                               seq.Items.Add (cce.Particle);\r
+                               seq.Compile (h, schema);\r
+                               seq.Validate (h, schema);\r
+                               validatableParticle = seq;\r
+                               resolvedContentType = GetComplexContentType (contentModel);\r
+                       }\r
+                       if (validatableParticle == null)\r
+                               validatableParticle = XmlSchemaParticle.Empty;\r
+               }\r
+\r
+               private void CollectContentTypeFromComplexRestriction ()\r
+               {\r
+                       XmlSchemaComplexContentRestriction ccr = (XmlSchemaComplexContentRestriction) ContentModel.Content;\r
+                       // 3.4.2 complex content schema component {content type}\r
+                       // - 1.1.1\r
+                       bool isEmptyParticle = false;\r
+                       if (ccr.Particle == null) \r
+                               isEmptyParticle = true;\r
+                       else {\r
+                               XmlSchemaGroupBase gb = ccr.Particle as XmlSchemaGroupBase;\r
+                               if (gb != null) {\r
+                                       // - 1.1.2\r
+                                       if (!(gb is XmlSchemaChoice) && gb.Items.Count == 0)\r
+                                               isEmptyParticle = true;\r
+                                       // - 1.1.3\r
+                                       else if (gb is XmlSchemaChoice && gb.Items.Count == 0 && gb.ValidatedMinOccurs == 0)\r
+                                               isEmptyParticle = true;\r
+                               }\r
+                       }\r
+                       if (isEmptyParticle) {\r
+                               resolvedContentType = XmlSchemaContentType.Empty;\r
+                               validatableParticle = XmlSchemaParticle.Empty;\r
+                       } else {\r
+                               // - 1.2.1\r
+                               resolvedContentType = GetComplexContentType (contentModel);\r
+                               // - 1.2.2\r
+                               validatableParticle = ccr.Particle;\r
+                       }\r
+               }\r
+\r
+               // 3.4.2 Complex Content Schema Component {content type} 1.2.1\r
+               private XmlSchemaContentType GetComplexContentType (XmlSchemaContentModel content)\r
+               {\r
+                       if (this.IsMixed || ((XmlSchemaComplexContent) content).IsMixed)\r
+                               return XmlSchemaContentType.Mixed;\r
+                       else\r
+                               return XmlSchemaContentType.ElementOnly;\r
+               }\r
+               #endregion\r
+\r
+               //\r
+               // We have to validate:\r
+               //\r
+               //      - 3.4.3 Complex Type Definition Representation OK\r
+               //      - 3.4.6 Type Definition Properties Correct\r
+               //      - 3.4.6 Derivation Valid (Extension)\r
+               //      - 3.4.6 Derivation Valid (Restriction, Complex)\r
+               //\r
+               // There are many schema errata:\r
+               // http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
+               //\r
+               // E1-43 Derivation Valid (Restriction, Complex) 5.\r
+               // E1-21 Derivation Valid (Restriction, Complex) 4.3.\r
+               // E1-17 Type Derivation OK (Complex) 2.1.\r
+               //\r
+               // And E1-38, E1-37, E1-30, E1-27\r
+               //\r
+               internal override int Validate (ValidationEventHandler h, XmlSchema schema)\r
                {\r
                        if (IsValidated (schema.ValidationId))\r
                                return errorCount;\r
@@ -367,50 +596,38 @@ namespace System.Xml.Schema
                        // it may result in insufficient results.\r
                        ValidationId = schema.ValidationId;\r
 \r
-                       // Term. 1 of 3.4.6 = 3.4.1 : Complex Type Definitions Properties Correct\r
-                       // Term. 2 and 3 goes ValidateContentModel().\r
-                       // Term. 4 and 5 follows in this method.\r
-                       //\r
-                       // Schema component to CLR type property mapping:\r
-                       // {derivation method} => resolvedDerivedBy\r
-                       // {annotations} are as is.\r
-                       // {name}, {namespace} => QualifiedName\r
-                       // {final} and {prohibited substitutions} are Compile()d.\r
-                       // {abstract} => ValidatedIsAbstract\r
-\r
-                       // Below are different properties depending on simpleContent | complexContent.\r
-                       // {base type definition} => BaseSchemaType (later)\r
-                       // {attribute uses} => AttributeUses (later)\r
-                       // {content type} => ContentType and ContentTypeParticle (later)\r
+                       CollectSchemaComponent (h, schema);\r
 \r
-                       // TODO: Beware of E1-27 of http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
-                       if (contentModel != null)\r
+                       // 3.4.6: Properties Correct\r
+                       // Term. 1 => 3.4.1 already done by CollectSchemaComponent()\r
+                       //            except for {attribute uses} and {attribute wildcard}\r
+                       // Term. 2, 3 and 4 goes to ValidateContentModel().\r
+                       // Term. 5 follows in this method.\r
+                       //\r
+                       if (ContentModel != null)\r
                                ValidateContentModel (h, schema);\r
                        else {\r
-                               if (particle != null)\r
-                                       ValidateParticle (h, schema);\r
-                               // contentModel never has them.\r
+                               if (Particle != null)\r
+                                       ValidateImmediateParticle (h, schema);\r
                                ValidateImmediateAttributes (h, schema);\r
                        }\r
+\r
                        // Additional support for 3.8.6 All Group Limited\r
-                       if (contentTypeParticle != null) {\r
-                               XmlSchemaAll termAll = contentTypeParticle.ActualParticle as XmlSchemaAll;\r
-                               if (termAll != null && contentTypeParticle.ValidatedMaxOccurs != 1)\r
+                       if (ContentTypeParticle != null) {\r
+                               XmlSchemaAll termAll = contentTypeParticle.GetOptimizedParticle (true) as XmlSchemaAll;\r
+                               if (termAll != null && (termAll.ValidatedMaxOccurs != 1 || contentTypeParticle.ValidatedMaxOccurs != 1)) // here contentTypeParticle is used to check occurence.\r
                                        error (h, "Particle whose term is -all- and consists of complex type content particle must have maxOccurs = 1.");\r
                        }\r
 \r
-                       // {content type} is going to be finished.\r
-                       if (contentTypeParticle == null)\r
-                               contentTypeParticle = XmlSchemaParticle.Empty;\r
                        contentTypeParticle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),\r
                                new ArrayList (), h, schema);\r
                        contentTypeParticle.ValidateUniqueTypeAttribution (\r
                                new XmlSchemaObjectTable (), h, schema);\r
-                       resolvedContentType = GetContentType ();\r
 \r
                        // 3.4.6 Properties Correct :: 5 (Two distinct ID attributes)\r
                        XmlSchemaAttribute idAttr = null;\r
-                       foreach (XmlSchemaAttribute attr in attributeUses) {\r
+                       foreach (DictionaryEntry entry in attributeUses) {\r
+                               XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
                                XmlSchemaDatatype dt = attr.AttributeType as XmlSchemaDatatype;\r
                                if (dt != null && dt.TokenizedType != XmlTokenizedType.ID)\r
                                        continue;\r
@@ -428,11 +645,9 @@ namespace System.Xml.Schema
                        return errorCount;\r
                }\r
 \r
-               private void ValidateParticle (ValidationEventHandler h, XmlSchema schema)\r
+               private void ValidateImmediateParticle (ValidationEventHandler h, XmlSchema schema)\r
                {\r
-                       // {content type} as a particle.\r
                        errorCount += particle.Validate (h, schema);\r
-                       contentTypeParticle = Particle;\r
                        XmlSchemaGroupRef pgrp = Particle as XmlSchemaGroupRef;\r
                        if (pgrp != null) {\r
                                if (pgrp.TargetGroup != null)\r
@@ -454,6 +669,8 @@ namespace System.Xml.Schema
 \r
                private void ValidateContentModel (ValidationEventHandler h, XmlSchema schema)\r
                {\r
+                       XmlSchemaType baseType = BaseXmlSchemaTypeInternal;\r
+\r
                        // Here we check 3.4.6 Properties Correct :: 2. and 3.\r
                        errorCount += contentModel.Validate (h, schema);\r
                        XmlSchemaComplexContentExtension cce = contentModel.Content as XmlSchemaComplexContentExtension;\r
@@ -464,44 +681,21 @@ namespace System.Xml.Schema
                        XmlSchemaAnyAttribute localAnyAttribute = null;\r
                        XmlSchemaAnyAttribute baseAnyAttribute = null;\r
 \r
-                       XmlQualifiedName baseTypeName = null;\r
-                       if (cce != null)\r
-                               baseTypeName = cce.BaseTypeName;\r
-                       else if (ccr != null)\r
-                               baseTypeName = ccr.BaseTypeName;\r
-                       else if (sce != null)\r
-                               baseTypeName = sce.BaseTypeName;\r
-                       else\r
-                               baseTypeName = scr.BaseTypeName;\r
-\r
-                       XmlSchemaType baseType = schema.SchemaTypes [baseTypeName] as XmlSchemaType;\r
-                       // Resolve redefine.\r
-                       if (this.isRedefineChild && baseType != null && this.QualifiedName == baseTypeName) {\r
-                               baseType = (XmlSchemaType) redefinedObject;\r
-                               if (baseType == null)\r
-                                       error (h, "Redefinition base type was not found.");\r
-                               baseType.Validate (h, schema);\r
-                       }\r
                        // 3.4.6 Properties Correct :: 3. Circular definition prohibited.\r
                        if (ValidateRecursionCheck ())\r
                                error (h, "Circular definition of schema types was found.");\r
                        if (baseType != null) {\r
                                baseType.Validate (h, schema);\r
                                // Fill "Datatype" property.\r
-                               this.datatypeInternal = baseType.Datatype;\r
-                       } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName)\r
-                               datatypeInternal = XmlSchemaSimpleType.AnySimpleType;
-                       else if (baseTypeName.Namespace == XmlSchema.Namespace) {\r
-                               datatypeInternal = XmlSchemaDatatype.FromName (baseTypeName);\r
+                               this.DatatypeInternal = baseType.Datatype;\r
+                       } else if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
+                               DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
+                       else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName)) {\r
+                               DatatypeInternal = XmlSchemaDatatype.FromName (BaseSchemaTypeName);\r
                        }\r
 \r
-                       // {derivation method}\r
                        XmlSchemaComplexType baseComplexType = baseType as XmlSchemaComplexType;\r
                        XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;\r
-                       if (cce != null || sce != null)\r
-                               resolvedDerivedBy = XmlSchemaDerivationMethod.Extension;\r
-                       else\r
-                               resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;\r
 \r
                        // 3.4.6 Derivation Valid (common to Extension and Restriction, Complex) :: 1.\r
                        if (baseType != null && (baseType.FinalResolved & resolvedDerivedBy) != 0)\r
@@ -516,62 +710,65 @@ namespace System.Xml.Schema
                        if (cce != null || ccr != null) {\r
                                // 3.4.3 Complex Type Definition Representation OK :: 1.\r
                                // base\r
-                               if (baseTypeName == XmlSchemaComplexType.AnyTypeName)\r
+                               if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
                                        baseComplexType = XmlSchemaComplexType.AnyType;\r
-                               else if (baseTypeName.Namespace == XmlSchema.Namespace)\r
+                               else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName))\r
                                        error (h, "Referenced base schema type is XML Schema datatype.");\r
-                               else if (baseComplexType == null && !schema.IsNamespaceAbsent (baseTypeName.Namespace))\r
-                                       error (h, "Referenced base schema type " + baseTypeName + " was not complex type or not found in the corresponding schema.");\r
+                               else if (baseComplexType == null && !schema.IsNamespaceAbsent (BaseSchemaTypeName.Namespace))\r
+                                       error (h, "Referenced base schema type " + BaseSchemaTypeName + " was not complex type or not found in the corresponding schema.");\r
                        }\r
                        // Common to simpleContent \r
                        else {\r
+                               // ContentType of {content type}\r
+                               resolvedContentType = XmlSchemaContentType.TextOnly;\r
+\r
                                // 3.4.3 Complex Type Definition Representation OK :: 1.\r
                                // base\r
-                               if (baseTypeName == XmlSchemaComplexType.AnyTypeName)\r
+                               if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
                                        baseComplexType = XmlSchemaComplexType.AnyType;
 \r
                                if (baseComplexType != null && baseComplexType.ContentType != XmlSchemaContentType.TextOnly) {\r
-                                       error (h, "Base schema complex type of a simple content must be simple content type. Base type is " + baseTypeName);\r
-                               } else if (sce == null && (baseSimpleType != null && baseTypeName.Namespace != XmlSchema.Namespace)) {\r
-                                       error (h, "If a simple content is not an extension, base schema type must be complex type. Base type is " + baseTypeName);\r
-                               } else if (baseTypeName.Namespace == XmlSchema.Namespace) {\r
-                                       if (XmlSchemaDatatype.FromName (baseTypeName) == null)\r
-                                               error (h, "Invalid schema data type was specified: " + baseTypeName);\r
+                                       error (h, "Base schema complex type of a simple content must be simple content type. Base type is " + BaseSchemaTypeName);\r
+                               } else if (sce == null && (baseSimpleType != null && BaseSchemaTypeName.Namespace != XmlSchema.Namespace)) {\r
+                                       error (h, "If a simple content is not an extension, base schema type must be complex type. Base type is " + BaseSchemaTypeName);\r
+                               } else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName)) {\r
                                        // do nothing for particle.\r
                                }\r
                                // otherwise, it might be missing sub components.\r
-                               else if (baseType == null && !schema.IsNamespaceAbsent (baseTypeName.Namespace))// && schema.Schemas [baseTypeName.Namespace] != null)\r
-                                       error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");\r
+                               else if (baseType == null && !schema.IsNamespaceAbsent (BaseSchemaTypeName.Namespace))\r
+                                       error (h, "Referenced base schema type " + BaseSchemaTypeName + " was not found in the corresponding schema.");\r
+\r
+                               // 3.4.3 Complex Type Definition Representation OK :: 2.\r
+                               // Note that baseSimpleType is also allowed as to Errata E1-27 (http://www.w3.org/2001/05/xmlschema-errata)\r
+                               if (baseComplexType != null) {\r
+                                       if (baseComplexType.ContentType == XmlSchemaContentType.TextOnly) {\r
+                                               // 2.1.1\r
+                                       // Here "baseComplexType.Particle != null" is required for error-ignorant case\r
+                                       } else if (scr != null && baseComplexType.ContentType == XmlSchemaContentType.Mixed && baseComplexType.Particle != null && baseComplexType.Particle.ValidateIsEmptiable () && scr.BaseType != null) {\r
+                                               // 2.1.2 && 2.2: OK\r
+                                       }\r
+                                       else\r
+                                               error (h, "Base complex type of a simple content restriction must be text only.");\r
+                               } else {\r
+                                       if (sce != null && baseComplexType == null) {\r
+                                               // 2.1.3 : OK\r
+                                       }\r
+                                       else\r
+                                               error (h, "Not allowed base type of a simple content restriction.");\r
+                               }\r
                        }\r
 \r
                        // complexType/complexContent/extension\r
                        if (cce != null) {\r
-                               // ContentTypeParticle\r
-                               if (baseComplexType == null) {\r
-                                       // Basically it is an error. Considering ValidationEventHandler.\r
-                               }\r
-                               else if (baseComplexType.ContentTypeParticle == XmlSchemaParticle.Empty\r
-                                       || baseComplexType == XmlSchemaComplexType.AnyType)\r
-                                       contentTypeParticle = cce.Particle;\r
-                               else if (cce.Particle == null || cce.Particle == XmlSchemaParticle.Empty)\r
-                                       contentTypeParticle = baseComplexType.ContentTypeParticle;\r
-                               else {\r
-                                       // create a new sequences that merges both contents.\r
-                                       XmlSchemaSequence seq = new XmlSchemaSequence ();\r
-                                       seq.Items.Add (baseComplexType.ContentTypeParticle);\r
-                                       seq.Items.Add (cce.Particle);\r
-                                       seq.Compile (h, schema);\r
-                                       seq.Validate (h, schema);\r
-                                       contentTypeParticle = seq;\r
-                               }\r
-\r
                                // I don't think 3.4.6 Derivation Valid (Extension) :: 1.2\r
                                // is constraining anything here, since 3.4.2 {attribute uses}\r
                                // defines as to include base type's attribute uses.\r
                                localAnyAttribute = cce.AnyAttribute;\r
                                if (baseComplexType != null) {\r
-                                       foreach (XmlSchemaAttribute attr in baseComplexType.AttributeUses)\r
+                                       foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
+                                               XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
                                                XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
+                                       }\r
                                        baseAnyAttribute = baseComplexType.AttributeWildcard;\r
                                }\r
                                // attributes\r
@@ -590,15 +787,10 @@ namespace System.Xml.Schema
                                // For ValidationEventHandler.\r
                                if (baseComplexType == null)\r
                                        baseComplexType = XmlSchemaComplexType.AnyType;\r
-\r
-                               // ContentTypeParticles (It must contain base type's particle).\r
-                               if (ccr.Particle != null) {\r
+                               if (ccr.Particle != null)\r
                                        ccr.Particle.Validate (h, schema);\r
-                                       contentTypeParticle = ccr.Particle;\r
-                               }\r
-                               else\r
-                                       contentTypeParticle = XmlSchemaParticle.Empty;\r
 \r
+                               // attributes\r
                                localAnyAttribute = ccr.AnyAttribute;\r
                                this.attributeWildcard = localAnyAttribute;\r
                                if (baseComplexType != null)\r
@@ -607,12 +799,13 @@ namespace System.Xml.Schema
                                        // 1.3 attribute wildcard subset. (=> 3.10.6)\r
                                        localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
 \r
-                               // FIXME: Check 3.4.2 Complex Type Definition with complex content Schema Component\r
-                               // and its {attribute uses} and {attribute wildcard}\r
+                               // 3.4.2 Complex Type Definition with complex content Schema Component\r
+                               // and its {attribute uses} and {attribute wildcard} are done here (descendantly)\r
                                errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
                                        this.attributeUses, h, schema, ccr.Attributes, \r
                                        ccr.AnyAttribute, ref attributeWildcard, null);\r
-                               foreach (XmlSchemaAttribute attr in baseComplexType.AttributeUses) {\r
+                               foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
+                                       XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
                                        if (attributeUses [attr.QualifiedName] == null)\r
                                                XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
                                }\r
@@ -637,8 +830,14 @@ namespace System.Xml.Schema
                                if (baseComplexType != null) {\r
                                        baseAnyAttribute = baseComplexType.AttributeWildcard;\r
 \r
-                                       foreach (XmlSchemaAttribute attr in baseComplexType.AttributeUses)\r
+                                       foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
+                                               XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
+#if BUGGY_MS_COMPLIANT\r
+                                               if (attr.Use != XmlSchemaUse.Prohibited)\r
+                                                       XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
+#endif\r
                                                XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
+                                       }\r
                                }\r
                                if (baseAnyAttribute != null && localAnyAttribute != null)\r
                                        // 1.3 attribute wildcard subset. (=> 3.10.6)\r
@@ -646,96 +845,64 @@ namespace System.Xml.Schema
                        }\r
                        // complexType/simpleContent/restriction\r
                        if (scr != null) {\r
-                               if (baseComplexType == null) {\r
-                                       // 3.4.3 :: 2.\r
-                                       error (h, "Base type of a simple content restriction must be a complex type.");\r
-                               } else {\r
-                                       // Attributes\r
-                                       baseAnyAttribute = baseComplexType.AttributeWildcard;\r
+                               // Attributes\r
+                               baseAnyAttribute = baseComplexType != null ? baseComplexType.AttributeWildcard : null;\r
 \r
-                                       localAnyAttribute = scr.AnyAttribute;\r
-                                       if (localAnyAttribute != null && baseAnyAttribute != null)\r
-                                               // 1.3 attribute wildcard subset. (=> 3.10.6)\r
-                                               localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
-                                       // TODO: 3.4.6 :: 5.1. Beware that There is an errata for 5.1!!\r
-                                       // http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
-\r
-                                       // FIXME: Check 3.4.2 Complex Type Definition with simple content Schema Component\r
-                                       // and its {attribute uses} and {attribute wildcard}\r
-                                       errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
-                                               this.attributeUses, h, schema, scr.Attributes, \r
-                                               scr.AnyAttribute, ref attributeWildcard, null);\r
-                               }\r
+                               localAnyAttribute = scr.AnyAttribute;\r
+                               if (localAnyAttribute != null && baseAnyAttribute != null)\r
+                                       // 1.3 attribute wildcard subset. (=> 3.10.6)\r
+                                       localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
+                               // 3.4.6 :: 5.1. Beware that There is an errata for 5.1!!\r
+                               // http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
+\r
+                               // 3.4.2 Complex Type Definition with simple content Schema Component\r
+                               // and its {attribute uses} and {attribute wildcard} are done here (descendantly)\r
+                               errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
+                                       this.attributeUses, h, schema, scr.Attributes, \r
+                                       scr.AnyAttribute, ref attributeWildcard, null);\r
                        }\r
 \r
                        // Common process of AttributeWildcard.\r
-                       // TODO: Check 3.4.2 {attribute wildcard} to fill the complete wildcard.\r
                        if (localAnyAttribute != null) {\r
                                this.attributeWildcard = localAnyAttribute;\r
                        }\r
                        else\r
                                this.attributeWildcard = baseAnyAttribute;\r
-                       this.baseSchemaTypeInternal = baseType;\r
-               }\r
-\r
-               private void AddExtensionAttributes (XmlSchemaObjectCollection attributes,\r
-                       XmlSchemaAnyAttribute anyAttribute, ValidationEventHandler h, XmlSchema schema)\r
-               {\r
-               }\r
-\r
-               // It was formerly placed directly in ContentType property.\r
-               // I get it out, since ContentType is _post_ compilation property value.\r
-               private XmlSchemaContentType GetContentType ()\r
-               {\r
-                       if (this.isMixed)\r
-                               return XmlSchemaContentType.Mixed;\r
-                       XmlSchemaComplexContent xcc = \r
-                               ContentModel as XmlSchemaComplexContent;\r
-                       if (xcc != null && xcc.IsMixed)\r
-                               return XmlSchemaContentType.Mixed;\r
-\r
-                       XmlSchemaSimpleContent xsc = ContentModel as XmlSchemaSimpleContent;\r
-                       if (xsc != null)\r
-                               return XmlSchemaContentType.TextOnly;\r
-\r
-                       return contentTypeParticle != XmlSchemaParticle.Empty ?\r
-                               XmlSchemaContentType.ElementOnly :\r
-                               XmlSchemaContentType.Empty;\r
                }\r
 \r
                // 3.4.6 Type Derivation OK (Complex)\r
                internal void ValidateTypeDerivationOK (object b, ValidationEventHandler h, XmlSchema schema)\r
                {\r
                        // AnyType derives from AnyType itself.\r
-                       if (this == XmlSchemaComplexType.AnyType && BaseSchemaType == this)\r
+                       if (this == XmlSchemaComplexType.AnyType && BaseXmlSchemaType == this)\r
                                return;\r
 \r
                        XmlSchemaType bst = b as XmlSchemaType;\r
                        if (b == this)  // 1 and 2.1\r
                                return;\r
-                       if (bst != null && (resolvedDerivedBy & bst.FinalResolved) != 0) // 1\r
+                       if (bst != null && (resolvedDerivedBy & bst.FinalResolved) != 0) // 1.\r
                                error (h, "Derivation type " + resolvedDerivedBy + " is prohibited by the base type.");\r
-                       if (BaseSchemaType == b) // 2.2\r
+                       if (BaseXmlSchemaType == b) // 2.2\r
                                return;\r
-                       if (BaseSchemaType == XmlSchemaComplexType.AnyType) { // 2.3.1\r
+                       if (BaseXmlSchemaType == null || BaseXmlSchemaType == XmlSchemaComplexType.AnyType) { // 2.3.1\r
                                error (h, "Derived type's base schema type is anyType.");\r
                                return;\r
                        }\r
                        // 2.3.2.1\r
-                       XmlSchemaComplexType dbct = BaseSchemaType as XmlSchemaComplexType;\r
+                       XmlSchemaComplexType dbct = BaseXmlSchemaType as XmlSchemaComplexType;\r
                        if (dbct != null) {\r
                                dbct.ValidateTypeDerivationOK (b, h, schema);\r
                                return;\r
                        }\r
                        // 2.3.2.2\r
-                       XmlSchemaSimpleType dbst = BaseSchemaType as XmlSchemaSimpleType;\r
+                       XmlSchemaSimpleType dbst = BaseXmlSchemaType as XmlSchemaSimpleType;\r
                        if (dbst != null) {\r
                                dbst.ValidateTypeDerivationOK (b, h, schema, true);\r
                                return;\r
                        }\r
                }\r
 \r
-               // Term. 1 of 3.4.6 Derivation Valid (Extension)\r
+               // 3.4.6 Derivation Valid (Extension) - Term. 1 (Complex Type)\r
                internal void ValidateComplexBaseDerivationValidExtension (XmlSchemaComplexType baseComplexType,\r
                        ValidationEventHandler h, XmlSchema schema)\r
                {\r
@@ -743,11 +910,11 @@ namespace System.Xml.Schema
                        if ((baseComplexType.FinalResolved & XmlSchemaDerivationMethod.Extension) != 0)\r
                                error (h, "Derivation by extension is prohibited.");\r
                        // 1.2\r
-                       foreach (XmlSchemaAttribute ba in baseComplexType.AttributeUses) {\r
+                       foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
+                               XmlSchemaAttribute ba = (XmlSchemaAttribute) entry.Value;\r
                                XmlSchemaAttribute da = AttributeUses [ba.QualifiedName] as XmlSchemaAttribute;\r
                                if (da == null)\r
                                        error (h, "Invalid complex type derivation by extension was found. Missing attribute was found: " + ba.QualifiedName + " .");\r
-                               // TODO: How to evaluate "equal" type ...?\r
                        }\r
                        // 1.3 -> 3.10.6 Wildcard Subset.\r
                        if (AnyAttribute != null) {\r
@@ -764,29 +931,24 @@ namespace System.Xml.Schema
                        // 1.4.2.2.1\r
                        if (baseComplexType.ContentType != XmlSchemaContentType.Empty) {\r
                                // 1.4.2.2.2.1\r
-                               if (this.GetContentType () == baseComplexType.ContentType) {\r
-                                       // nothing to do\r
-                               }\r
-                               // 1.4.2.2.2.2\r
-                               // 3.9.6 Particle Valid (Extension)\r
-                               else if (this.contentTypeParticle != baseComplexType.ContentTypeParticle) {\r
+                               if (this.ContentType != baseComplexType.ContentType)\r
+//                             if (this.GetContentType (false) != baseComplexType.GetContentType (false))\r
+                                       error (h, "Base complex type has different content type " + baseComplexType.ContentType + ".");\r
+                               // 1.4.2.2.2.2 => 3.9.6 Particle Valid (Extension)\r
+                               else if (this.contentTypeParticle == null ||\r
+                                       !this.contentTypeParticle.ParticleEquals (baseComplexType.ContentTypeParticle)) {\r
                                        XmlSchemaSequence seq = contentTypeParticle as XmlSchemaSequence;\r
-                                       if (contentTypeParticle.ValidatedMinOccurs != 1 ||\r
-                                               contentTypeParticle.ValidatedMaxOccurs != 1 ||\r
-                                               seq == null)\r
+                                       if (contentTypeParticle != XmlSchemaParticle.Empty && (seq == null || contentTypeParticle.ValidatedMinOccurs != 1 || contentTypeParticle.ValidatedMaxOccurs != 1))\r
                                                error (h, "Invalid complex content extension was found.");\r
                                        else {\r
                                                // Identical sequence item should be checked, but\r
                                                // I think it is naturally achieved as coded above.\r
                                        }\r
-\r
                                }\r
-                               else\r
-                                       error (h, "Invalid complex content extension was found. Extended complex type has different content type from base type.");\r
                        }\r
                }\r
 \r
-               // Term. 2 of 3.4.6 Derivation Valid (Extension)\r
+               // 3.4.6 Derivation Valid (Extension) - Term. 2 (Simple Type)\r
                internal void ValidateSimpleBaseDerivationValidExtension (object baseType,\r
                        ValidationEventHandler h, XmlSchema schema)\r
                {\r
@@ -799,25 +961,6 @@ namespace System.Xml.Schema
                                dt = st.Datatype;\r
                        if (dt != this.Datatype)\r
                                error (h, "To extend simple type, a complex type must have the same content type as the base type.");\r
-\r
-                       /*\r
-                       switch (resolvedContentType) {\r
-                       case XmlSchemaContentType.Mixed:\r
-                       case XmlSchemaContentType.TextOnly:\r
-                               XmlSchemaSimpleType st = baseType as XmlSchemaSimpleType;\r
-                               if ((st == null && Datatype != baseType) ||\r
-                                       (st != null && st.Datatype != Datatype))\r
-                                       goto case XmlSchemaContentType.ElementOnly;\r
-                               if (st != null\r
-                                       && (st.FinalResolved & XmlSchemaDerivationMethod.Extension) != 0)\r
-                                       error (h, "Extension is prohibited by the base type.");\r
-                               break;\r
-                       case XmlSchemaContentType.ElementOnly:\r
-                       case XmlSchemaContentType.Empty:\r
-                               error (h, "To extend simple type, a complex type must have the same content type as the base type.");\r
-                               break;\r
-                       }\r
-                       */\r
                }\r
 \r
                internal void ValidateDerivationValidRestriction (XmlSchemaComplexType baseType,\r
@@ -834,7 +977,8 @@ namespace System.Xml.Schema
                        }\r
 \r
                        // 2.\r
-                       foreach (XmlSchemaAttribute attr in this.AttributeUses) {\r
+                       foreach (DictionaryEntry entry in this.AttributeUses) {\r
+                               XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
                                XmlSchemaAttribute baseAttr = baseType.AttributeUses [attr.QualifiedName] as XmlSchemaAttribute;\r
                                if (baseAttr != null) {\r
                                        // 2.1\r
@@ -871,7 +1015,7 @@ namespace System.Xml.Schema
                        }\r
                        // I think 3. is considered in 2.\r
                        // 4.\r
-                       if (this.AttributeWildcard != null) {\r
+                       if (this.AttributeWildcard != null && baseType != XmlSchemaComplexType.AnyType) {\r
                                if (baseType.AttributeWildcard == null)\r
                                        error (h, "Invalid attribute derivation by restriction because of attribute wildcard.");\r
                                else\r
@@ -879,20 +1023,30 @@ namespace System.Xml.Schema
                        }\r
 \r
                        // 5.\r
+                       if (this == AnyType)\r
+                               return;\r
                        if (contentTypeParticle == XmlSchemaParticle.Empty) {\r
-                               // TODO: 5.1\r
-                               // 5.2\r
-                               if (baseType.ContentTypeParticle != XmlSchemaParticle.Empty &&\r
-                                       !baseType.ContentTypeParticle.ValidateIsEmptiable ())\r
-                               error (h, "Invalid content type derivation.");\r
+                               // 5.1\r
+                               if (ContentType != XmlSchemaContentType.Empty) {\r
+                                       // TODO: 5.1.1\r
+//                                     XmlSchemaSimpleType baseST = baseType as XmlSchemaSimpleType;\r
+                                       // 5.1.2\r
+                                       if (baseType.ContentType == XmlSchemaContentType.Mixed && !baseType.ContentTypeParticle.ValidateIsEmptiable ())\r
+                                               error (h, "Invalid content type derivation.");\r
+\r
+                               } else {\r
+                                       // 5.2\r
+                                       if (baseType.ContentTypeParticle != XmlSchemaParticle.Empty &&\r
+                                               !baseType.ContentTypeParticle.ValidateIsEmptiable ())\r
+                                               error (h, "Invalid content type derivation.");\r
+                               }\r
                        } else {\r
                                // 5.3 => 3.9.6 Particle Valid (Restriction)\r
                                if (baseType.ContentTypeParticle != null) {\r
                                        // 3.9.6 - 1 : same particle.\r
                                        // 3.9.6 - 2 is covered by using ActualParticle.\r
-                                       if (!contentTypeParticle.ActualParticle.ParticleEquals (baseType.ContentTypeParticle.ActualParticle))\r
-                                               contentTypeParticle.ActualParticle.ValidateDerivationByRestriction (\r
-                                                       baseType.ContentTypeParticle.ActualParticle, h, schema);\r
+                                       if (!contentTypeParticle.ParticleEquals (baseType.ContentTypeParticle))\r
+                                               contentTypeParticle.ValidateDerivationByRestriction (baseType.ContentTypeParticle, h, schema, true);\r
                                }\r
                        }\r
                }\r
@@ -938,14 +1092,14 @@ namespace System.Xml.Schema
                                        ctype.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",\r
                                                XmlSchemaUtil.ComplexTypeBlockAllowed);\r
                                        if(innerex != null)\r
-                                               warn(h,"some invalid values for block attribute were found",innerex);\r
+                                               error (h,"some invalid values for block attribute were found",innerex);\r
                                }\r
                                else if(reader.Name == "final")\r
                                {\r
                                        ctype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",\r
                                                XmlSchemaUtil.FinalAllowed);\r
                                        if(innerex != null)\r
-                                               warn(h,"some invalid values for final attribute were found",innerex);\r
+                                               error (h,"some invalid values for final attribute were found",innerex);\r
                                }\r
                                else if(reader.Name == "id")\r
                                {\r