Forráskód Böngészése

2004-01-21 Atsushi Enomoto <[email protected]>

	* XmlSchema.cs :
	  - Removed extra property Language.  Fill substitution-group info
	    before ContentTypeParticle is computed.
	* XmlSchemaComplexType.cs : ContentType and ContentTypeParticle are
	  now computer before Validate() (to exclude 3.4.2 from 3.4.6
	  component constraint check).
	* XmlSchemaObject.cs, XmlSchemaParticle.cs :
	  Added CopyInfo() for cloning objects.
	* XmlSchemaObjectTable.cs :
	  Add() doesn't have to reject same named items.
	* XmlSchemaParticle.cs :
	  - Renamed internal class XmlSchemaParticleEmpty to EmptyParticle
	    (for ease of post compilation infoset comparison).
	  - GetMinEffectiveTotalRange() should return max occurs for element and
	    any.
	* XmlSchemaParticle.cs,
	  XmlSchemaAll.cs,
	  XmlSchemaAny.cs,
	  XmlSchemaChoice.cs,
	  XmlSchemaElement.cs,
	  XmlSchemaGroupBase.cs,
	  XmlSchemaGroupRef.cs,
	  XmlSchemaSequence.cs :
	  - Removed ActualParticle property.
	  - Added GetOptimizedParticle() to get ContentTypeParticle.
	  - ContentTypeParticle is now created as different object from
	    user-specified "Particle" property.
	  - Return Empty particle when model groups contains no Items.
	  - When Items contains exactly 1 item, return the item (unless it is
	    top level choice model group - it causes DBR problem).
	  - ValidateDerivationValidRestriction() and related WXS 3.9.6 DBR
	    check methods such as ValidateNSRecurseCheckCardinality() now
	    returns a bool value and avoids exceptions on each failure time.
	  - ValidateRecurse() now doesn't progress base particle incorrectly.
	    (it caused incorrect order-preserving-mapping error.)
	  - 3.9.6 Recurse, RecurseLax, MapAndSum are now treated as different.
	* XmlSchemaAll.cs : occurence validation shoud be done against values
	  after computation (ValidatedMaxOccurs).
	* XmlSchemaChoice.cs : When Items is empty, then invoke warning event.
	* XmlSchemaElement.cs :
	  - Consider referencedElement also for SubstitutionGroupElement.
	  - Removed SubstitutingChoice which is not in use.
	  - when it was targeted as substitution group by other elements, its
	    ContentTypeParticle should be substitution choice (see 3.9.6
	    Particle Valid (Restriction) term 2.1).
	  - Added FillsubstitutionElementInfo() (see XmlSchema.cs above).
	* XmlSchemaGroup.cs : Removed extra check on model group (because it
	  is impossible to specify non-model-group particle).
	* XmlSchemaIdentityConstraint.cs,
	  XmlSchemaKey.cs,
	  XmlSchemaKeyRef.cs,
	  XmlSchemaUnique.cs : Removed extraneous MonoTODO.
	* XmlSchemaSimpleContent.cs : Made actualSchemaType as internal.
	* XmlSchemaSimpleType.cs : Removed extraneous new Datatype property.
	* XmlSchemaSimpleTypeRestriction.cs : Renamed actualBaseSchemaType to
	  ActualBaseSchemaType.
	* XmlSchemaSimpleTypeList.cs, XmlSchemaSimpleTypeUnion.cs :
	  Removed unused actualBaseSchemaType code.
	* XmlSchemaType.cs : use recently introduced BaseXmlSchemaType.
	* XmlSchemaUtil.cs : Removed unused CheckLanguage().

svn path=/trunk/mcs/; revision=22323
Atsushi Eno 22 éve
szülő
commit
ccd5f38514
24 módosított fájl, 726 hozzáadás és 336 törlés
  1. 63 0
      mcs/class/System.XML/System.Xml.Schema/ChangeLog
  2. 7 10
      mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs
  3. 53 24
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaAll.cs
  4. 29 7
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaAny.cs
  5. 114 26
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaChoice.cs
  6. 26 14
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaComplexType.cs
  7. 131 58
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaElement.cs
  8. 1 18
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroup.cs
  9. 87 39
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroupBase.cs
  10. 45 19
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroupRef.cs
  11. 0 1
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaIdentityConstraint.cs
  12. 0 1
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaKey.cs
  13. 0 2
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaKeyref.cs
  14. 10 0
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaObject.cs
  15. 6 9
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaObjectTable.cs
  16. 52 26
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaParticle.cs
  17. 85 44
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaSequence.cs
  18. 0 3
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleContent.cs
  19. 0 13
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleType.cs
  20. 1 3
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleTypeList.cs
  21. 11 12
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleTypeRestriction.cs
  22. 1 1
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaType.cs
  23. 0 2
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaUnique.cs
  24. 4 4
      mcs/class/System.XML/System.Xml.Schema/XmlSchemaUtil.cs

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

@@ -1,3 +1,66 @@
+2004-01-21  Atsushi Enomoto <[email protected]>
+
+	* XmlSchema.cs :
+	  - Removed extra property Language.  Fill substitution-group info
+	    before ContentTypeParticle is computed.
+	* XmlSchemaComplexType.cs : ContentType and ContentTypeParticle are 
+	  now computer before Validate() (to exclude 3.4.2 from 3.4.6
+	  component constraint check).
+	* XmlSchemaObject.cs, XmlSchemaParticle.cs :
+	  Added CopyInfo() for cloning objects.
+	* XmlSchemaObjectTable.cs :
+	  Add() doesn't have to reject same named items.
+	* XmlSchemaParticle.cs :
+	  - Renamed internal class XmlSchemaParticleEmpty to EmptyParticle 
+	    (for ease of post compilation infoset comparison).
+	  - GetMinEffectiveTotalRange() should return max occurs for element and
+	    any.
+	* XmlSchemaParticle.cs,
+	  XmlSchemaAll.cs,
+	  XmlSchemaAny.cs,
+	  XmlSchemaChoice.cs,
+	  XmlSchemaElement.cs,
+	  XmlSchemaGroupBase.cs,
+	  XmlSchemaGroupRef.cs,
+	  XmlSchemaSequence.cs :
+	  - Removed ActualParticle property.
+	  - Added GetOptimizedParticle() to get ContentTypeParticle.
+	  - ContentTypeParticle is now created as different object from 
+	    user-specified "Particle" property.
+	  - Return Empty particle when model groups contains no Items.
+	  - When Items contains exactly 1 item, return the item (unless it is
+	    top level choice model group - it causes DBR problem).
+	  - ValidateDerivationValidRestriction() and related WXS 3.9.6 DBR
+	    check methods such as ValidateNSRecurseCheckCardinality() now
+	    returns a bool value and avoids exceptions on each failure time.
+	  - ValidateRecurse() now doesn't progress base particle incorrectly.
+	    (it caused incorrect order-preserving-mapping error.)
+	  - 3.9.6 Recurse, RecurseLax, MapAndSum are now treated as different.
+	* XmlSchemaAll.cs : occurence validation shoud be done against values
+	  after computation (ValidatedMaxOccurs).
+	* XmlSchemaChoice.cs : When Items is empty, then invoke warning event.
+	* XmlSchemaElement.cs :
+	  - Consider referencedElement also for SubstitutionGroupElement.  
+	  - Removed SubstitutingChoice which is not in use.
+	  - when it was targeted as substitution group by other elements, its 
+	    ContentTypeParticle should be substitution choice (see 3.9.6 
+	    Particle Valid (Restriction) term 2.1).
+	  - Added FillsubstitutionElementInfo() (see XmlSchema.cs above).
+	* XmlSchemaGroup.cs : Removed extra check on model group (because it
+	  is impossible to specify non-model-group particle).
+	* XmlSchemaIdentityConstraint.cs,
+	  XmlSchemaKey.cs,
+	  XmlSchemaKeyRef.cs,
+	  XmlSchemaUnique.cs : Removed extraneous MonoTODO.
+	* XmlSchemaSimpleContent.cs : Made actualSchemaType as internal.
+	* XmlSchemaSimpleType.cs : Removed extraneous new Datatype property.
+	* XmlSchemaSimpleTypeRestriction.cs : Renamed actualBaseSchemaType to
+	  ActualBaseSchemaType.
+	* XmlSchemaSimpleTypeList.cs, XmlSchemaSimpleTypeUnion.cs :
+	  Removed unused actualBaseSchemaType code.
+	* XmlSchemaType.cs : use recently introduced BaseXmlSchemaType.
+	* XmlSchemaUtil.cs : Removed unused CheckLanguage().
+
 2004-01-21  Atsushi Enomoto <[email protected]>
 
 	* XmlSchemaXPath.cs : wildcard with prefix (e.g. @foo:*) was

+ 7 - 10
mcs/class/System.XML/System.Xml.Schema/XmlSchema.cs

@@ -213,14 +213,6 @@ namespace System.Xml.Schema
                         get{ return notations;}
                 }
 
-                // New attribute defined in W3C schema element
-                [System.Xml.Serialization.XmlAttribute("xml:lang")]
-                public string Language
-                {
-                        get{ return  language; }
-                        set{ language = value; }
-                }
-
 		internal Hashtable IDCollection
 		{
 			get { return idCollection; }
@@ -329,8 +321,8 @@ namespace System.Xml.Schema
                                 error(handler, Version + "is not a valid value for version attribute of schema");
 
                         //6. xml:lang must be a language
-                        if(!XmlSchemaUtil.CheckLanguage(Language))
-                                error(handler, Language + " is not a valid language");
+//                      if(!XmlSchemaUtil.CheckLanguage(Language))
+//                              error(handler, Language + " is not a valid language");
 
                         // Compile the content of this schema
 
@@ -559,6 +551,11 @@ namespace System.Xml.Schema
                 {
 			ValidationId = CompilationId;
 
+			// Firstly Element needs to be filled their substitution group info
+                        foreach(XmlSchemaElement elem in Elements.Values)
+				elem.FillSubstitutionElementInfo ();
+
+			// Validate
                         foreach(XmlSchemaAttribute attr in Attributes.Values)
                         {
                                 errorCount += attr.Validate(handler, this);

+ 53 - 24
mcs/class/System.XML/System.Xml.Schema/XmlSchemaAll.cs

@@ -17,6 +17,7 @@ namespace System.Xml.Schema
 	/// </summary>
 	public class XmlSchemaAll : XmlSchemaGroupBase
 	{
+		private XmlSchema schema;
 		private XmlSchemaObjectCollection items;
 		private static string xmlname = "all";
 		private bool emptiable;
@@ -37,17 +38,6 @@ namespace System.Xml.Schema
 			get { return emptiable; }
 		}
 
-		internal override XmlSchemaParticle ActualParticle {
-			get {
-				if (CompiledItems.Count == 0)
-					return XmlSchemaParticle.Empty;
-				else if (CompiledItems.Count == 1)
-					return ((XmlSchemaParticle) CompiledItems [0]).ActualParticle;
-				else
-					return this;
-			}
-		}
-
 
 		/// <remarks>
 		/// 1. MaxOccurs must be one. (default is also one)
@@ -59,6 +49,8 @@ namespace System.Xml.Schema
 			if (this.IsComplied (schema.CompilationId))
 				return 0;
 
+			this.schema = schema;
+
 			//FIXME: Should we reset the values on error??
 			if(MaxOccurs != Decimal.One)
 				error(h,"maxOccurs must be 1");
@@ -73,9 +65,9 @@ namespace System.Xml.Schema
 				XmlSchemaElement elem = obj as XmlSchemaElement;
 				if(elem != null)
 				{
-					if(elem.MaxOccurs != Decimal.One && elem.MaxOccurs != Decimal.Zero)
+					if(elem.ValidatedMaxOccurs != Decimal.One && elem.ValidatedMaxOccurs != Decimal.Zero)
 					{
-						elem.error(h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
+						elem.error (h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
 					}
 					errorCount += elem.Compile(h, schema);
 				}
@@ -89,11 +81,46 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+			if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
+				OptimizedParticle = XmlSchemaParticle.Empty;
+				return OptimizedParticle;
+			}
+			else if (Items.Count == 1) {
+				if (ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
+					XmlSchemaSequence seq = new XmlSchemaSequence ();
+					this.CopyInfo (seq);
+					XmlSchemaParticle p = (XmlSchemaParticle) Items [0];
+					p = p.GetOptimizedParticle (false);
+					if (p == XmlSchemaParticle.Empty)
+						OptimizedParticle = p;
+					else {
+						seq.Items.Add (p);
+						seq.CompiledItems.Add (p);
+						seq.Compile (null, schema);
+//						seq.Validate (null, schema);
+						OptimizedParticle = seq;
+					}
+					return OptimizedParticle;
+				}
+			}
+
+			XmlSchemaAll all = new XmlSchemaAll ();
+			CopyInfo (all);
+			CopyOptimizedItems (all);
+			OptimizedParticle = all;
+
+			return OptimizedParticle;
+		}
+
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.CompilationId))
 				return errorCount;
-			this.CompileOccurence (h, schema);
+
 			// 3.8.6 All Group Limited :: 1.
 			// Beware that this section was corrected: E1-26 of http://www.w3.org/2001/05/xmlschema-errata#Errata1
 			if (!this.parentIsGroupDefinition && ValidatedMaxOccurs != 1)
@@ -107,7 +134,7 @@ namespace System.Xml.Schema
 					obj.ValidatedMaxOccurs != 1)
 					error (h, "MaxOccurs of a particle inside -all- compositor must be either 0 or 1.");
 				CompiledItems.Add (obj);
-				if (obj.MinOccurs > 0)
+				if (obj.ValidatedMinOccurs > 0)
 					emptiable = false;
 			}
 
@@ -115,23 +142,25 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			XmlSchemaAny any = baseParticle as XmlSchemaAny;
 			XmlSchemaAll derivedAll = baseParticle as XmlSchemaAll;
 			if (any != null) {
 				// NSRecurseCheckCardinality
-				this.ValidateNSRecurseCheckCardinality (any, h, schema);
-				return;
+				return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
 			} else if (derivedAll != null) {
 				// Recurse
-				ValidateOccurenceRangeOK (derivedAll, h, schema);
-				this.ValidateRecurse (derivedAll, h, schema);
-				return;
+				if (!ValidateOccurenceRangeOK (derivedAll, h, schema, raiseError))
+					return false;
+				return ValidateRecurse (derivedAll, h, schema, raiseError);
+			}
+			else {
+				if (raiseError)
+					error (h, "Invalid -all- particle derivation was found.");
+				return false;
 			}
-			else
-				error (h, "Invalid -all- particle derivation was found.");
 		}
 
 		internal override decimal GetMinEffectiveTotalRange ()

+ 29 - 7
mcs/class/System.XML/System.Xml.Schema/XmlSchemaAny.cs

@@ -42,6 +42,7 @@ namespace System.Xml.Schema
 		private string nameSpace;
 		private XmlSchemaContentProcessing processing;
 		private static string xmlname = "any";
+
 		private XsdWildcard wildcard;
 
 		public XmlSchemaAny()
@@ -64,7 +65,7 @@ namespace System.Xml.Schema
 			set{ processing = value; }
 		}
 
-		// Internal
+		// Post Compilation Schema Infoset
 		internal bool HasValueAny {
 			get { return wildcard.HasValueAny; }
 		}
@@ -125,6 +126,25 @@ namespace System.Xml.Schema
 			this.CompilationId = schema.CompilationId;
 			return errorCount;
 		}
+
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+			// Uncommenting this causes incorrect validation. 
+			// It will prevent UPA e.g. msxsdtest/Particles/particlesJf006.xsd
+//			if (ValidatedMaxOccurs == 0) {
+//				OptimizedParticle = XmlSchemaParticle.Empty;
+//				return OptimizedParticle;
+//			}
+
+			XmlSchemaAny any = new XmlSchemaAny ();
+			CopyInfo (any);
+			any.wildcard = this.wildcard;
+			OptimizedParticle = any;
+
+			return OptimizedParticle;
+		}
 		
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
@@ -161,17 +181,19 @@ namespace System.Xml.Schema
 			return wildcard.ExamineAttributeWildcardIntersection (other, h, schema);
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle, 
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle, 
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
 			if (baseAny == null) {
-				error (h, "Invalid particle derivation by restriction was found.");
-				return;
+				if (raiseError)
+					error (h, "Invalid particle derivation by restriction was found.");
+				return false;
 			}
 			// 3.9.6 Particle Derivation OK (Any:Any - NSSubset)
-			this.ValidateOccurenceRangeOK (baseParticle, h, schema);
-			wildcard.ValidateWildcardSubset (baseAny.wildcard, h, schema);
+			if (!ValidateOccurenceRangeOK (baseParticle, h, schema, raiseError))
+				return false;
+			return wildcard.ValidateWildcardSubset (baseAny.wildcard, h, schema, raiseError);
 		}
 
 

+ 114 - 26
mcs/class/System.XML/System.Xml.Schema/XmlSchemaChoice.cs

@@ -12,9 +12,6 @@ using System.Xml;
 
 namespace System.Xml.Schema
 {
-	/// <summary>
-	/// Summary description for XmlSchemaAll.
-	/// </summary>
 	public class XmlSchemaChoice : XmlSchemaGroupBase
 	{
 		private XmlSchemaObjectCollection items;
@@ -36,17 +33,6 @@ namespace System.Xml.Schema
 			get{ return items; }
 		}
 
-		internal override XmlSchemaParticle ActualParticle {
-			get {
-				if (this.ValidatedMinOccurs == 1 &&
-					this.ValidatedMaxOccurs == 1 &&
-					CompiledItems.Count == 1)
-					return ((XmlSchemaParticle) CompiledItems [0]).ActualParticle;
-				else
-					return this;
-			}
-		}
-
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
@@ -56,6 +42,9 @@ namespace System.Xml.Schema
 			XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
 			CompileOccurence (h, schema);
 
+			if (Items.Count == 0)
+				this.warn (h, "Empty choice is unsatisfiable if minOccurs not equals to 0");
+
 			foreach(XmlSchemaObject obj in Items)
 			{
 				if(obj is XmlSchemaElement ||
@@ -72,15 +61,53 @@ namespace System.Xml.Schema
 			this.CompilationId = schema.CompilationId;
 			return errorCount;
 		}
-		
-		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
+
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+
+			if (Items.Count == 0 || ValidatedMaxOccurs == 0)
+				OptimizedParticle = XmlSchemaParticle.Empty;
+			// FIXME: Regardless of isTop, it should remove pointless particle.
+			else if (!isTop && Items.Count == 1 && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1)
+				OptimizedParticle = ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
+			else {
+				XmlSchemaChoice c = new XmlSchemaChoice ();
+				CopyInfo (c);
+				for (int i = 0; i < Items.Count; i++) {
+					XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
+					p = p.GetOptimizedParticle (false);
+					if (p == XmlSchemaParticle.Empty)
+						continue;
+					else if (p is XmlSchemaChoice && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
+						XmlSchemaChoice pc = p as XmlSchemaChoice;
+						for (int ci = 0; ci < pc.Items.Count; ci++) {
+							c.Items.Add (pc.Items [ci]);
+							c.CompiledItems.Add (c.Items [ci]);
+						}
+					}
+					else {
+						c.Items.Add (p);
+						c.CompiledItems.Add (p);
+					}
+				}
+				if (c.Items.Count == 0)
+					OptimizedParticle = XmlSchemaParticle.Empty;
+				else
+					OptimizedParticle = c;
+			}
+			return OptimizedParticle;
+		}
+
+		internal override int Validate (ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.CompilationId))
 				return errorCount;
 
 			CompiledItems.Clear ();
 			foreach (XmlSchemaParticle p in Items) {
-				errorCount += p.Validate (h, schema);
+				errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
 				CompiledItems.Add (p);
 			}
 
@@ -88,30 +115,91 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			XmlSchemaAny any = baseParticle as XmlSchemaAny;
 			if (any != null) {
 				// NSRecurseCheckCardinality
-				this.ValidateNSRecurseCheckCardinality (any, h, schema);
-				return;
+				return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
 			}
 
 			XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
 			if (choice != null) {
 				// RecurseLax
-				this.ValidateOccurenceRangeOK (choice, h, schema);
+				if (!ValidateOccurenceRangeOK (choice, h, schema, raiseError))
+					return false;
 
 				// If it is totally optional, then ignore their contents.
 				if (choice.ValidatedMinOccurs == 0 && choice.ValidatedMaxOccurs == 0 &&
 					this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
-					return;
-				this.ValidateRecurse (choice, h, schema);
-				return;
+					return true;
+//				return ValidateRecurseLax (choice, h, schema, raiseError);
+				return this.ValidateSeqRecurseMapSumCommon (choice, h, schema, true, false, raiseError);
+			}
+
+			if (raiseError)
+				error (h, "Invalid choice derivation by restriction was found.");
+			return false;
+		}
+
+		private bool ValidateRecurseLax (XmlSchemaGroupBase baseGroup,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
+		{
+			int index = 0;
+			for (int i = 0; i < baseGroup.CompiledItems.Count; i++) {
+				XmlSchemaParticle pb = (XmlSchemaParticle) baseGroup.CompiledItems [i];
+				pb = pb.GetOptimizedParticle (false);
+				if (pb == XmlSchemaParticle.Empty)
+					continue;
+				XmlSchemaParticle pd = null;
+				while (this.CompiledItems.Count > index) {
+					pd = (XmlSchemaParticle) this.CompiledItems [index];
+					pd = pd.GetOptimizedParticle (false);
+					index++;
+					if (pd != XmlSchemaParticle.Empty)
+						break;
+				}
+				if (!ValidateParticleSection (ref index, pd, pb, h, schema, raiseError))
+					continue;
+			}
+			if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
+				if (raiseError)
+					error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
+				return false;
+			}
+			return true;
+		}
+
+		private bool ValidateParticleSection (ref int index, XmlSchemaParticle pd, XmlSchemaParticle pb, ValidationEventHandler h, XmlSchema schema, bool raiseError)
+		{
+			if (pd == pb) // they are same particle
+				return true;
+
+			if (pd != null) {
+				XmlSchemaElement el = pd as XmlSchemaElement;
+				XmlSchemaParticle pdx = pd;
+//				if (el != null && el.SubstitutingElements.Count > 0)
+//					pdx = el.SubstitutingChoice;
+
+				if (!pdx.ValidateDerivationByRestriction (pb, h, schema, false)) {
+					if (!pb.ValidateIsEmptiable ()) {
+						if (raiseError)
+							error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
+						return false;
+					}
+					else {
+						index--; // try the same derived particle and next base particle.
+						return false;
+					}
+				}
+			} else if (!pb.ValidateIsEmptiable ()) {
+				if (raiseError)
+					error (h, "Invalid particle derivation by restriction was found. Base schema particle has non-emptiable sub particle that is not mapped to the derived particle.");
+				return false;
 			}
 
-			error (h, "Invalid choice derivation by restriction was found.");
+			return true;
 		}
 
 		internal override decimal GetMinEffectiveTotalRange ()

+ 26 - 14
mcs/class/System.XML/System.Xml.Schema/XmlSchemaComplexType.cs

@@ -411,7 +411,9 @@ namespace System.Xml.Schema
 				CollectContentTypeFromContentModel (h, schema);
 			} else
 				CollectContentTypeFromImmediateContent ();
-			contentTypeParticle = validatableParticle;//.GetParticleWithoutPointless ();
+			contentTypeParticle = validatableParticle.GetOptimizedParticle (true);
+			if (contentTypeParticle == XmlSchemaParticle.Empty && resolvedContentType == XmlSchemaContentType.ElementOnly)
+				resolvedContentType = XmlSchemaContentType.Empty;
 
 			CollectProcessId = schema.CompilationId;
 		}
@@ -477,7 +479,7 @@ namespace System.Xml.Schema
 					validatableParticle = XmlSchemaParticle.Empty;
 					resolvedContentType = XmlSchemaContentType.Empty;
 				} else {
-					validatableParticle = baseComplexType.ContentTypeParticle;
+					validatableParticle = baseComplexType.ValidatableParticle;
 					resolvedContentType = baseComplexType.resolvedContentType;
 				}
 			} else if (baseComplexType.validatableParticle == XmlSchemaParticle.Empty
@@ -488,7 +490,8 @@ namespace System.Xml.Schema
 			} else {
 				// - 2.3 : create a new sequences that merges both contents.
 				XmlSchemaSequence seq = new XmlSchemaSequence ();
-				seq.Items.Add (baseComplexType.ContentTypeParticle);
+				this.CopyInfo (seq);
+				seq.Items.Add (baseComplexType.validatableParticle);
 				seq.Items.Add (cce.Particle);
 				seq.Compile (h, schema);
 				seq.Validate (h, schema);
@@ -582,8 +585,8 @@ namespace System.Xml.Schema
 			}
 
 			// Additional support for 3.8.6 All Group Limited
-			if (contentTypeParticle != null) {
-				XmlSchemaAll termAll = contentTypeParticle.ActualParticle as XmlSchemaAll;
+			if (ContentTypeParticle != null) {
+				XmlSchemaAll termAll = contentTypeParticle.GetOptimizedParticle (true) as XmlSchemaAll;
 				if (termAll != null && (termAll.ValidatedMaxOccurs != 1 || contentTypeParticle.ValidatedMaxOccurs != 1)) // here contentTypeParticle is used to check occurence. FIXME: In the future contentTypeParticle will remove group references.
 					error (h, "Particle whose term is -all- and consists of complex type content particle must have maxOccurs = 1.");
 			}
@@ -714,7 +717,8 @@ namespace System.Xml.Schema
 				if (baseComplexType != null) {
 					if (baseComplexType.ContentType == XmlSchemaContentType.TextOnly) {
 						// 2.1.1
-					} else if (scr != null && baseComplexType.ContentType == XmlSchemaContentType.Mixed && baseComplexType.Particle.ValidateIsEmptiable () && scr.BaseType != null) {
+					// Here "baseComplexType.Particle != null" is required for error-ignorant case
+					} else if (scr != null && baseComplexType.ContentType == XmlSchemaContentType.Mixed && baseComplexType.Particle != null && baseComplexType.Particle.ValidateIsEmptiable () && scr.BaseType != null) {
 						// 2.1.2 && 2.2: OK
 					}
 					else
@@ -992,19 +996,27 @@ namespace System.Xml.Schema
 
 			// 5.
 			if (contentTypeParticle == XmlSchemaParticle.Empty) {
-				// TODO: 5.1
-				// 5.2
-				if (baseType.ContentTypeParticle != XmlSchemaParticle.Empty &&
-					!baseType.ContentTypeParticle.ValidateIsEmptiable ())
-				error (h, "Invalid content type derivation.");
+				// 5.1
+				if (ContentType != XmlSchemaContentType.Empty) {
+					// TODO: 5.1.1
+//					XmlSchemaSimpleType baseST = baseType as XmlSchemaSimpleType;
+					// 5.1.2
+					if (baseType.ContentType == XmlSchemaContentType.Mixed && !baseType.ContentTypeParticle.ValidateIsEmptiable ())
+						error (h, "Invalid content type derivation.");
+
+				} else {
+					// 5.2
+					if (baseType.ContentTypeParticle != XmlSchemaParticle.Empty &&
+						!baseType.ContentTypeParticle.ValidateIsEmptiable ())
+						error (h, "Invalid content type derivation.");
+				}
 			} else {
 				// 5.3 => 3.9.6 Particle Valid (Restriction)
 				if (baseType.ContentTypeParticle != null) {
 					// 3.9.6 - 1 : same particle.
 					// 3.9.6 - 2 is covered by using ActualParticle.
-					if (!contentTypeParticle.ActualParticle.ParticleEquals (baseType.ContentTypeParticle.ActualParticle))
-						contentTypeParticle.ActualParticle.ValidateDerivationByRestriction (
-							baseType.ContentTypeParticle.ActualParticle, h, schema);
+					if (!contentTypeParticle.ParticleEquals (baseType.ContentTypeParticle))
+						contentTypeParticle.ValidateDerivationByRestriction (baseType.ContentTypeParticle, h, schema, true);
 				}
 			}
 		}

+ 131 - 58
mcs/class/System.XML/System.Xml.Schema/XmlSchemaElement.cs

@@ -255,36 +255,18 @@ namespace System.Xml.Schema
 		}
 
 		internal XmlSchemaElement SubstitutionGroupElement {
-			get { return substitutionGroupElement; }
+			get {
+				if (referencedElement != null)
+					return referencedElement.SubstitutionGroupElement;
+				else
+					return substitutionGroupElement;
+			}
 		}
 
 		#endregion
 
 		private XmlSchemaParticle substChoice;
 
-//		/*
-		// FIXME: using this causes stack overflow...
-		internal XmlSchemaParticle SubstitutingChoice {
-			get {
-				if (substChoice != null)
-					return substChoice;
-				else if (this.SubstitutingElements != null && this.SubstitutingElements.Count > 0) {
-					XmlSchemaChoice choice = new XmlSchemaChoice ();
-					substChoice = choice;
-					choice.Compile (null, schema); // compute Validated Min/Max Occurs.
-					choice.CompiledItems.Add (this);
-					for (int i = 0; i < SubstitutingElements.Count; i++) {
-						XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;
-						choice.CompiledItems.Add (se);
-					}
-				}
-				else
-					substChoice= this;
-				return substChoice;
-			}
-		}
-//		*/
-
 		/// <remarks>
 		/// a) If Element has parent as schema:
 		///		1. name must be present and of type NCName.
@@ -510,6 +492,72 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 
+		[MonoTODO ("Return clone in case when it returns itself")]
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+			if (RefName != null && RefName != XmlQualifiedName.Empty) {
+				referencedElement = schema.Elements [RefName] as XmlSchemaElement;
+			}
+
+//			if (this.referencedElement != null)
+//				OptimizedParticle = referencedElement.GetOptimizedParticle (isTop);
+//			else 
+			if (ValidatedMaxOccurs == 0)
+				OptimizedParticle = XmlSchemaParticle.Empty;
+			// Substitution Group
+			else if (SubstitutingElements != null && SubstitutingElements.Count > 0) {
+				XmlSchemaChoice choice = new XmlSchemaChoice ();
+				choice.MinOccurs = MinOccurs;
+				choice.MaxOccurs = MaxOccurs;
+				substChoice = choice;
+				choice.Compile (null, schema); // compute Validated Min/Max Occurs.
+				XmlSchemaElement item = this.MemberwiseClone () as XmlSchemaElement;
+				item.MinOccurs = 1;
+				item.MaxOccurs = 1;
+				item.substitutionGroupElement = null;
+				item.substitutingElements = null;
+				for (int i = 0; i < SubstitutingElements.Count; i++) {
+					XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;
+//					choice.Items.Add (se);
+//					choice.CompiledItems.Add (se);
+					this.AddSubstElementRecursively (choice.Items, se);
+					this.AddSubstElementRecursively (choice.CompiledItems, se);
+				}
+				if (!choice.Items.Contains (item)) {
+					choice.Items.Add (item);
+					choice.CompiledItems.Add (item);
+				}
+				OptimizedParticle = choice;
+			}
+			else
+				OptimizedParticle = this;//.MemberwiseClone () as XmlSchemaElement;
+			return OptimizedParticle;
+		}
+
+		private void AddSubstElementRecursively (XmlSchemaObjectCollection col, XmlSchemaElement el)
+		{
+			if (el.SubstitutingElements != null)
+				for (int i = 0; i < el.SubstitutingElements.Count; i++)
+					this.AddSubstElementRecursively (col, el.SubstitutingElements [i] as XmlSchemaElement);
+			if (!col.Contains (el))
+				col.Add (el);
+		}
+
+		internal void FillSubstitutionElementInfo ()
+		{
+			if (this.substitutionGroupElement != null)
+				return;
+
+			if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
+				XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
+				this.substitutionGroupElement = substElem;
+				if (substElem != null)
+					substElem.substitutingElements.Add (this);
+			}
+		}
+
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.CompilationId))
@@ -531,6 +579,13 @@ namespace System.Xml.Schema
 			this.actualIsNillable = IsNillable;
 			this.actualIsAbstract = IsAbstract;
 
+			// Before determining element type, we need to validate substituting element
+			if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
+				XmlSchemaElement substElem = substitutionGroupElement;
+				if (substElem != null)
+					substElem.Validate (h, schema);
+			}
+
 			// {type} from here
 			XmlSchemaDatatype datatype = null;
 			if (schemaType != null)
@@ -568,9 +623,14 @@ namespace System.Xml.Schema
 					error (h, "Referenced element " + RefName + " was not found in the corresponding schema.");
 			}
 			
-			// Otherwise the -ur type- definition.
-			if (elementType == null)
-				elementType = XmlSchemaComplexType.AnyType;
+			// Otherwise if there are substitution group, then the type of the substitution group element.
+			if (referencedElement == null) {
+				if (elementType == null && this.substitutionGroupElement != null)
+					elementType = substitutionGroupElement.ElementType;
+				// Otherwise, the -ur type- definition.
+				if (elementType == null)
+					elementType = XmlSchemaComplexType.AnyType;
+			}
 
 			XmlSchemaType xsType = elementType as XmlSchemaType;
 			if (xsType != null) {
@@ -582,10 +642,9 @@ namespace System.Xml.Schema
 			// {substitution group affiliation}
 			// 3. subsitution group's type derivation check.
 			if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
-				XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
+				XmlSchemaElement substElem = substitutionGroupElement;
 				// If el is null, then it is missing sub components .
 				if (substElem != null) {
-					substElem.Validate (h, schema);
 					XmlSchemaType substSchemaType = substElem.ElementType as XmlSchemaType;
 					if (substSchemaType != null) {
 						// 3.3.6 Properties Correct 3.
@@ -603,8 +662,6 @@ namespace System.Xml.Schema
 							xsSimpleType.ValidateTypeDerivationOK (substElem.ElementType, h, schema, true);
 					}
 
-					this.substitutionGroupElement = substElem;
-					substElem.substitutingElements.Add (this);
 				}
 				// otherwise, it might be missing sub components.
 				else if (!schema.IsNamespaceAbsent (SubstitutionGroup.Namespace))
@@ -662,23 +719,22 @@ namespace System.Xml.Schema
 			return true;
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			// element - NameAndTypeOK
 			XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
 			if (baseElement != null) {
-				ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema);
-				return;
+				return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);
 			}
 
 			// any - NSCompat
 			XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
 			if (baseAny != null) {
 				// NSCompat
-				baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, true);
-				ValidateOccurenceRangeOK (baseAny, h, schema);
-				return;
+				if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))
+					return false;
+				return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);
 			}
 
 //*
@@ -697,49 +753,66 @@ namespace System.Xml.Schema
 				gb.Validate (h, schema);
 				// It looks weird, but here we never think about 
 				// _pointlessness_ of this groupbase particle.
-				gb.ValidateDerivationByRestriction (baseParticle, h, schema);
-				return;
+				return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
 			}
 //*/
+			return true;
 		}
 
-		private void ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
-			ValidationEventHandler h, XmlSchema schema)
+		private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			// 1.
-			if (this.QualifiedName != baseElement.QualifiedName)
-				error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
+			if (this.QualifiedName != baseElement.QualifiedName) {
+				if (raiseError)
+					error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
+				return false;
+			}
 			// 2.
-			if (this.isNillable && !baseElement.isNillable)
-				error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
+			if (this.isNillable && !baseElement.isNillable) {
+				if (raiseError)
+					error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
+				return false;
+			}
 			// 3.
-			ValidateOccurenceRangeOK (baseElement, h, schema);
+			if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))
+				return false;
 			// 4.
 			if (baseElement.ValidatedFixedValue != null &&
-				baseElement.ValidatedFixedValue != this.ValidatedFixedValue)
-				error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
+				baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {
+				if (raiseError)
+					error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
+				return false;
+			}
 			// 5. TODO: What is "identity constraints subset" ???
 
 			// 6. 
-			if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved)
-				error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
+			if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {
+				if (raiseError)
+					error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
+				return false;
+			}
 			// 7.
 			if (baseElement.ElementType != null) {
 				XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
-				if (derivedCType != null)
-					// W3C REC says that it is Type Derivation OK to be check, but
+				if (derivedCType != null) {
+					// FIXME: W3C REC says that it is Type Derivation OK to be check, but
 					// in fact it should be DerivationValid (Restriction, Complex).
 					derivedCType.ValidateDerivationValidRestriction (
 						baseElement.ElementType as XmlSchemaComplexType, h, schema);
-					// derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
-				else {
+					derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
+				} else {
 					XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
 					if (derivedSType != null)
 						derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
-					else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType)
-						error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
+					else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {
+						if (raiseError)
+							error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
+						return false;
+					}
 				}
 			}
+			return true;
 		}
 
 		internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
@@ -753,7 +826,7 @@ namespace System.Xml.Schema
 		internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
 			ValidationEventHandler h, XmlSchema schema)
 		{
-			if (qnames.Contains (this.QualifiedName))
+			if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))
 				error (h, "Ambiguous element label was detected: " + this.QualifiedName);
 			else {
 				foreach (XmlSchemaAny any in nsNames) {

+ 1 - 18
mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroup.cs

@@ -57,7 +57,6 @@ namespace System.Xml.Schema
 
 		// 1. name must be present
 		// 2. MinOccurs & MaxOccurs of the Particle must be absent
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
@@ -82,22 +81,7 @@ namespace System.Xml.Schema
 				if(Particle.MinOccursString != null)
 					Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
 			
-				if(Particle is XmlSchemaChoice)
-				{
-					errorCount += ((XmlSchemaChoice)Particle).Compile(h,schema);
-				}
-				else if(Particle is XmlSchemaSequence)
-				{
-					errorCount += ((XmlSchemaSequence)Particle).Compile(h,schema);
-				}
-				else if(Particle is XmlSchemaAll)
-				{
-					errorCount += ((XmlSchemaAll)Particle).Compile(h,schema);
-				}
-				else
-				{
-					error(h,"only all,choice or sequence are allowed");
-				}
+				Particle.Compile (h, schema);
 			}
 			
 			XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
@@ -106,7 +90,6 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 		
-		[MonoTODO]
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (this.IsValidated (schema.ValidationId))

+ 87 - 39
mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroupBase.cs

@@ -27,6 +27,18 @@ namespace System.Xml.Schema
 			get{ return compiledItems; }
 		}
 
+		internal void CopyOptimizedItems (XmlSchemaGroupBase gb)
+		{
+			for (int i = 0; i < Items.Count; i++) {
+				XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
+				p = p.GetOptimizedParticle (false);
+				if (p == XmlSchemaParticle.Empty)
+					continue;
+				gb.Items.Add (p);
+				gb.CompiledItems.Add (p);
+			}
+		}
+
 		internal override bool ParticleEquals (XmlSchemaParticle other)
 		{
 			XmlSchemaGroupBase gb = other as XmlSchemaGroupBase;
@@ -55,58 +67,94 @@ namespace System.Xml.Schema
 				p.CheckRecursion (depth, h, schema);
 		}
 
-		internal void ValidateNSRecurseCheckCardinality (XmlSchemaAny any,
-			ValidationEventHandler h, XmlSchema schema)
+		internal bool ValidateNSRecurseCheckCardinality (XmlSchemaAny any,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			foreach (XmlSchemaParticle p in Items)
-				p.ValidateDerivationByRestriction (any, h, schema);
-			ValidateOccurenceRangeOK (any, h, schema);
+				if (!p.ValidateDerivationByRestriction (any, h, schema, raiseError))
+					return false;
+			return ValidateOccurenceRangeOK (any, h, schema, raiseError);
 		}
 
-		internal void ValidateRecurse (XmlSchemaGroupBase baseGroup,
-			ValidationEventHandler h, XmlSchema schema)
+		internal bool ValidateRecurse (XmlSchemaGroupBase baseGroup,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
+		{
+			return ValidateSeqRecurseMapSumCommon (baseGroup, h, schema, false, false, raiseError);
+		}
+
+		internal bool ValidateSeqRecurseMapSumCommon (XmlSchemaGroupBase baseGroup,
+			ValidationEventHandler h, XmlSchema schema, bool isLax, bool isMapAndSum, bool raiseError)
 		{
 			int index = 0;
-			for (int i = 0; i < baseGroup.CompiledItems.Count; i++) {
-				XmlSchemaParticle pb = ((XmlSchemaParticle) baseGroup.CompiledItems [i]).ActualParticle;
-				if (pb == XmlSchemaParticle.Empty)
-					continue;
+			int baseIndex = 0;
+			decimal baseOccured = 0;
+			if (baseGroup.CompiledItems.Count == 0 && this.CompiledItems.Count > 0) {
+				if (raiseError)
+					error (h, "Invalid particle derivation by restriction was found. base particle does not contain particles.");
+				return false;
+			}
+
+			for (int i = 0; i < CompiledItems.Count; i++) {
+				// get non-empty derived particle
 				XmlSchemaParticle pd = null;
 				while (this.CompiledItems.Count > index) {
-					pd = ((XmlSchemaParticle) this.CompiledItems [index]).ActualParticle;
-					index++;
-					if (pd != XmlSchemaParticle.Empty)
+					pd = ((XmlSchemaParticle) this.CompiledItems [index]).GetOptimizedParticle (false);
+					if (pd != XmlSchemaParticle.Empty)// && pd.ValidatedMaxOccurs > 0)
 						break;
+					else
+						index++;
+				}
+				if (index >= CompiledItems.Count) {
+					if (raiseError)
+						error (h, "Invalid particle derivation by restriction was found. Cannot be mapped to base particle.");
+					return false;
 				}
-				ValidateParticleSection (ref index, pd, pb, h, schema);
-			}
-			if (this.compiledItems.Count > 0 && index != this.CompiledItems.Count)
-				error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
-		}
-
-		private void ValidateParticleSection (ref int index, XmlSchemaParticle pd, XmlSchemaParticle pb, ValidationEventHandler h, XmlSchema schema)
-		{
-			if (pd == pb) // they are same particle
-				return;
-
-			if (pd != null) {
-				try {
-					XmlSchemaElement el = pd as XmlSchemaElement;
-					XmlSchemaParticle pdx = pd;
-					if (el != null && el.SubstitutingElements.Count > 0)
-						pdx = el.SubstitutingChoice;
 
-					pdx.ValidateDerivationByRestriction (pb, h, schema);
-				} catch (XmlSchemaException ex) {
-					if (!pb.ValidateIsEmptiable ())
-						error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.", ex);
-					else
-						index--; // try the same derived particle and next base particle.
+				// get non-empty base particle
+				XmlSchemaParticle pb = null;
+				while (baseGroup.CompiledItems.Count > baseIndex) {
+					pb = ((XmlSchemaParticle) baseGroup.CompiledItems [baseIndex]).GetOptimizedParticle (false);
+					if (pb == XmlSchemaParticle.Empty && pb.ValidatedMaxOccurs > 0)
+						continue;
+					if (!pd.ValidateDerivationByRestriction (pb, h, schema, false)) {
+						if (!isLax && !isMapAndSum && pb.MinOccurs > baseOccured && !pb.ValidateIsEmptiable ()) {
+							if (raiseError)
+								error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
+							return false;
+						}
+						else {
+							baseOccured = 0;
+							baseIndex++;
+						}
+					} else {
+						baseOccured += pb.ValidatedMinOccurs;
+						if (baseOccured >= baseGroup.ValidatedMaxOccurs) {
+							baseOccured = 0;
+							baseIndex++;
+						}
+						index++;
+						break;
+					}
 				}
-			} else if (!pb.ValidateIsEmptiable ()) {
-				error (h, "Invalid particle derivation by restriction was found. Base schema particle has non-emptiable sub particle that is not mapped to the derived particle.");
-				return;
 			}
+			if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
+				if (raiseError)
+					error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
+				return false;
+			}
+			if (!isLax && !isMapAndSum) {
+				if (baseOccured > 0)
+					baseIndex++;
+				for (int i = baseIndex; i < baseGroup.CompiledItems.Count; i++) {
+					XmlSchemaParticle p = baseGroup.CompiledItems [i] as XmlSchemaParticle;
+					if (!p.ValidateIsEmptiable ()) {
+						if (raiseError)
+							error (h, "Invalid particle derivation by restriction was found. There is a base particle which does not have mapped derived particle and is not emptiable.");
+						return false;
+					}
+				}
+			}
+			return true;
 		}
 	}
 }

+ 45 - 19
mcs/class/System.XML/System.Xml.Schema/XmlSchemaGroupRef.cs

@@ -17,9 +17,8 @@ namespace System.Xml.Schema
 	/// </summary>
 	public class XmlSchemaGroupRef : XmlSchemaParticle
 	{
-		private XmlSchemaGroupBase particle;
+		private XmlSchema schema;
 		private XmlQualifiedName refName;
-		private XmlQualifiedName resolvedRefName;
 		private static string xmlname = "group";
 		private XmlSchemaGroup referencedGroup;
 
@@ -27,12 +26,16 @@ namespace System.Xml.Schema
 		{
 			refName = XmlQualifiedName.Empty;
 		}
+
+		// Attribute
 		[System.Xml.Serialization.XmlAttribute("ref")]
 		public XmlQualifiedName RefName 
 		{
 			get{ return  refName; } 
 			set{ refName = value; }
 		}
+
+		// Post Compilation Schema Information
 		[XmlIgnore]
 		public XmlSchemaGroupBase Particle 
 		{
@@ -43,6 +46,7 @@ namespace System.Xml.Schema
 					return null;
 			}
 		}
+
 		internal XmlSchemaGroup TargetGroup
 		{
 			get {
@@ -52,26 +56,16 @@ namespace System.Xml.Schema
 					return referencedGroup;
 			}
 		}
-		internal override XmlSchemaParticle ActualParticle
-		{
-			get {
-				if (TargetGroup != null)
-					return TargetGroup.Particle.ActualParticle;
-				else
-					// For ValidationEventHandler and missing sub components.
-					return XmlSchemaParticle.Empty;
-			}
-		}
 
 		/// <remarks>
 		/// 1. RefName must be present
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
 			if (this.IsComplied (schema.CompilationId))
 				return 0;
+			this.schema = schema;
 
 			XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
 			CompileOccurence (h, schema);
@@ -87,7 +81,6 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 		
-		[MonoTODO]
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.ValidationId))
@@ -97,23 +90,54 @@ namespace System.Xml.Schema
 			// it might be missing sub components.
 			if (referencedGroup == null && !schema.IsNamespaceAbsent (RefName.Namespace))
 				error (h, "Referenced group " + RefName + " was not found in the corresponding schema.");
-			else if (TargetGroup != null)
+			// See Errata E1-26: minOccurs=0 is now allowed.
+			else if (referencedGroup.Particle is XmlSchemaAll && ValidatedMaxOccurs != 1)
+				error (h, "Group reference to -all- particle must have schema component {maxOccurs}=1.");
+			if (TargetGroup != null)
 				TargetGroup.Validate (h, schema);
 
 			ValidationId = schema.ValidationId;
 			return errorCount;
 		}
 
+		bool busy; // only for avoiding infinite loop on illegal recursion cases.
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (busy)
+				return XmlSchemaParticle.Empty;
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+			busy = true;
+			XmlSchemaGroup g = referencedGroup != null ? referencedGroup : schema.Groups [RefName] as XmlSchemaGroup;
+			if (g != null && g.Particle != null) {
+				OptimizedParticle = g.Particle;
+				if (OptimizedParticle != XmlSchemaParticle.Empty && (ValidatedMinOccurs != 1 || ValidatedMaxOccurs != 1)) {
+					OptimizedParticle = OptimizedParticle.GetShallowClone ();
+					OptimizedParticle.MinOccurs = this.MinOccurs;
+					OptimizedParticle.MaxOccurs = this.MaxOccurs;
+					OptimizedParticle.CompileOccurence (null, null);
+				}
+				OptimizedParticle = OptimizedParticle.GetOptimizedParticle (isTop);
+			}
+			else
+				OptimizedParticle = XmlSchemaParticle.Empty;
+			busy = false;
+			return OptimizedParticle;
+		}
+
+
 		internal override bool ParticleEquals (XmlSchemaParticle other)
 		{
-			return ActualParticle.ParticleEquals (other.ActualParticle);
+			return this.GetOptimizedParticle (true).ParticleEquals (other);
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			if (TargetGroup != null)
-				TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema);
+				return TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
+			else
+				return false; // should not occur
 		}
 
 
@@ -145,6 +169,7 @@ namespace System.Xml.Schema
 		}
 
 
+		#region Read
 		//	<group 
 		//		 id = ID 
 		//		 ref = QName
@@ -239,5 +264,6 @@ namespace System.Xml.Schema
 			}			
 			return groupref;
 		}
+		#endregion
 	}
 }

+ 0 - 1
mcs/class/System.XML/System.Xml.Schema/XmlSchemaIdentityConstraint.cs

@@ -61,7 +61,6 @@ namespace System.Xml.Schema
 		/// 1. name must be present
 		/// 2. selector and field must be present
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.

+ 0 - 1
mcs/class/System.XML/System.Xml.Schema/XmlSchemaKey.cs

@@ -19,7 +19,6 @@ namespace System.Xml.Schema
 		/// 1. name must be present
 		/// 2. selector and field must be present
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			return base.Compile(h, schema);

+ 0 - 2
mcs/class/System.XML/System.Xml.Schema/XmlSchemaKeyref.cs

@@ -37,7 +37,6 @@ namespace System.Xml.Schema
 		/// 2. selector and field must be present
 		/// 3. refer must be present
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			base.Compile(h, schema);
@@ -50,7 +49,6 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 		
-		[MonoTODO]
 		internal override int Validate (ValidationEventHandler h, XmlSchema schema)
 		{
 			// Find target key

+ 10 - 0
mcs/class/System.XML/System.Xml.Schema/XmlSchemaObject.cs

@@ -131,5 +131,15 @@ namespace System.Xml.Schema
 		{
 			return this.ValidationId == validationId;
 		}
+
+		// This method is used only by particles
+		internal virtual void CopyInfo (XmlSchemaParticle obj)
+		{
+			obj.LineNumber = LineNumber;
+			obj.LinePosition = LinePosition;
+			obj.SourceUri = SourceUri;
+			obj.errorCount = errorCount;
+			// Other fields and properties may be useless for Particle.
+		}
 	}
 }

+ 6 - 9
mcs/class/System.XML/System.Xml.Schema/XmlSchemaObjectTable.cs

@@ -13,7 +13,7 @@ namespace System.Xml.Schema
 	{
 		private Hashtable table;
 
-		internal XmlSchemaObjectTable()
+		internal XmlSchemaObjectTable ()
 		{
 			table = new Hashtable(); 
 		}
@@ -34,20 +34,17 @@ namespace System.Xml.Schema
 			get{ return table.Values; }
 		}
 
-		public bool Contains(XmlQualifiedName name)
+		public bool Contains (XmlQualifiedName name)
 		{
-			return table.Contains(name);
+			return table.Contains (name);
 		}
-		public IDictionaryEnumerator GetEnumerator()
+		public IDictionaryEnumerator GetEnumerator ()
 		{
 			return new XmlSchemaObjectTableEnumerator (this);
 		}
 
-		internal void Add(XmlQualifiedName name, XmlSchemaObject value)
+		internal void Add (XmlQualifiedName name, XmlSchemaObject value)
 		{
-			if (table.ContainsKey (name))
-				throw new XmlSchemaException (
-					"Schema object for the name " + name + " already exists in this table.", value, null);
 			table [name] = value;
 		}
 
@@ -56,7 +53,7 @@ namespace System.Xml.Schema
 			table.Clear ();
 		}
 
-		internal void Set(XmlQualifiedName name, XmlSchemaObject value)
+		internal void Set (XmlQualifiedName name, XmlSchemaObject value)
 		{
 			table [name] = value;
 		}

+ 52 - 26
mcs/class/System.XML/System.Xml.Schema/XmlSchemaParticle.cs

@@ -11,23 +11,23 @@ namespace System.Xml.Schema
 	/// </summary>
 	public abstract class XmlSchemaParticle : XmlSchemaAnnotated
 	{
-		decimal minOccurs, maxOccurs;
-		string  minstr, maxstr;
-		static XmlSchemaParticle empty;
-		decimal validatedMinOccurs, validatedMaxOccurs;
-		internal int recursionDepth = -1;
-		private decimal minEffectiveTotalRange = -1;
-		internal bool parentIsGroupDefinition;
-
 		internal static XmlSchemaParticle Empty {
 			get {
 				if (empty == null) {
-					empty = new XmlSchemaParticleEmpty ();
+					empty = new EmptyParticle ();
 				}
 				return empty;
 			}
 		}
 
+		decimal minOccurs, maxOccurs;
+		string  minstr, maxstr;
+		static XmlSchemaParticle empty;
+		decimal validatedMinOccurs = 1, validatedMaxOccurs = 1;
+		internal int recursionDepth = -1;
+		private decimal minEffectiveTotalRange = -1;
+		internal bool parentIsGroupDefinition;
+
 		protected XmlSchemaParticle()
 		{
 			minOccurs = decimal.One;
@@ -125,12 +125,16 @@ namespace System.Xml.Schema
 		{
 			get { return validatedMaxOccurs; }
 		}
+		#endregion
 
-		internal virtual XmlSchemaParticle ActualParticle
+		internal XmlSchemaParticle OptimizedParticle;
+
+		internal abstract XmlSchemaParticle GetOptimizedParticle (bool isTop);
+
+		internal XmlSchemaParticle GetShallowClone ()
 		{
-			get { return this; }
+			return (XmlSchemaParticle) MemberwiseClone ();
 		}
-		#endregion
 
 		internal void CompileOccurence (ValidationEventHandler h, XmlSchema schema)
 		{
@@ -148,18 +152,35 @@ namespace System.Xml.Schema
 			}
 		}
 
-		internal virtual void ValidateOccurenceRangeOK (XmlSchemaParticle other,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override void CopyInfo (XmlSchemaParticle obj)
+		{
+			base.CopyInfo (obj);
+			if (MaxOccursString == "unbounded")
+				obj.MaxOccurs = obj.validatedMaxOccurs = decimal.MaxValue;
+			else 
+				obj.MaxOccurs = obj.validatedMaxOccurs = this.ValidatedMaxOccurs;
+			if (MaxOccurs == 0)
+				obj.MinOccurs = obj.validatedMinOccurs = 0;
+			else
+				obj.MinOccurs = obj.validatedMinOccurs = this.ValidatedMinOccurs;
+		}
+
+		internal virtual bool ValidateOccurenceRangeOK (XmlSchemaParticle other,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			if ((this.ValidatedMinOccurs < other.ValidatedMinOccurs) ||
 				(other.ValidatedMaxOccurs != decimal.MaxValue &&
-				this.ValidatedMaxOccurs > other.ValidatedMaxOccurs))
-				error (h, "Invalid derivation occurence range was found.");
+				this.ValidatedMaxOccurs > other.ValidatedMaxOccurs)) {
+				if (raiseError)
+					error (h, "Invalid derivation occurence range was found.");
+				return false;
+			}
+			return true;
 		}
 
 		internal virtual decimal GetMinEffectiveTotalRange ()
 		{
-			return 0;
+			return ValidatedMinOccurs;
 		}
 
 		internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
@@ -180,13 +201,14 @@ namespace System.Xml.Schema
 			return product;
 		}
 
+		// 3.9.6 Particle Emptiable
 		internal virtual bool ValidateIsEmptiable ()
 		{
 			return this.validatedMinOccurs == 0 || this.GetMinEffectiveTotalRange () == 0;
 		}
 
-		internal abstract void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema);
+		internal abstract bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError);
 
 		internal abstract void ValidateUniqueParticleAttribution (
 			XmlSchemaObjectTable qnames, ArrayList nsNames,
@@ -201,22 +223,26 @@ namespace System.Xml.Schema
 		internal abstract bool ParticleEquals (XmlSchemaParticle other);
 
 		#region Internal Class
-		public class XmlSchemaParticleEmpty : XmlSchemaParticle
+		public class EmptyParticle : XmlSchemaParticle
 		{
-			internal XmlSchemaParticleEmpty ()
+			internal EmptyParticle ()
 			{
 			}
 
-			internal override bool ParticleEquals (XmlSchemaParticle other)
+			internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
 			{
-				return other == this || other is XmlSchemaParticleEmpty;
+				return this;
 			}
 
+			internal override bool ParticleEquals (XmlSchemaParticle other)
+			{
+				return other == this || other == XmlSchemaParticle.Empty;
+			}
 
-			internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-				ValidationEventHandler h, XmlSchema schema)
+			internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+				ValidationEventHandler h, XmlSchema schema, bool raiseError)
 			{
-				// TODO
+				return true;
 			}
 
 			internal override void CheckRecursion (int depth, 

+ 85 - 44
mcs/class/System.XML/System.Xml.Schema/XmlSchemaSequence.cs

@@ -35,20 +35,6 @@ namespace System.Xml.Schema
 			get{ return items; }
 		}
 
-		internal override XmlSchemaParticle ActualParticle {
-			get {
-				if (CompiledItems.Count == 0)
-					return XmlSchemaParticle.Empty;
-				if (ValidatedMaxOccurs == 1 &&
-					ValidatedMinOccurs == 1 &&
-					CompiledItems.Count == 1)
-					return ((XmlSchemaParticle) CompiledItems [0]).ActualParticle;
-				else
-					return this;
-			}
-		}
-
-
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
@@ -74,84 +60,139 @@ namespace System.Xml.Schema
 			this.CompilationId = schema.CompilationId;
 			return errorCount;
 		}
-		
-		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
+
+
+		internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
+		{
+			if (OptimizedParticle != null)
+				return OptimizedParticle;
+			if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
+				OptimizedParticle = XmlSchemaParticle.Empty;
+				return OptimizedParticle;
+			}
+			if (!isTop && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
+				if (Items.Count == 1)
+					return ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
+			}
+
+			XmlSchemaSequence seq = new XmlSchemaSequence ();
+			CopyInfo (seq);
+			for (int i = 0; i < Items.Count; i++) {
+				XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
+				p = p.GetOptimizedParticle (false);
+				if (p == XmlSchemaParticle.Empty)
+					continue;
+
+				else if (p is XmlSchemaSequence && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
+					XmlSchemaSequence ps = p as XmlSchemaSequence;
+					for (int pi = 0; pi < ps.Items.Count; pi++) {
+						seq.Items.Add (ps.Items [pi]);
+						seq.CompiledItems.Add (ps.Items [pi]);
+					}
+				}
+				else {
+					seq.Items.Add (p);
+					seq.CompiledItems.Add (p);
+				}
+			}
+			if (seq.Items.Count == 0)
+				OptimizedParticle = XmlSchemaParticle.Empty;
+			else
+				OptimizedParticle = seq;
+			return OptimizedParticle;
+		}
+
+		internal override int Validate (ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.CompilationId))
 				return errorCount;
 
 			CompiledItems.Clear ();
-			foreach (XmlSchemaObject obj in Items) {
-				errorCount += obj.Validate (h, schema);
-				CompiledItems.Add (obj);
+			foreach (XmlSchemaParticle p in Items) {
+				errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
+//				XmlSchemaParticle particleInPoint = p.GetParticleWithoutPointless ();
+//				if (particleInPoint != XmlSchemaParticle.Empty)
+//					CompiledItems.Add (particleInPoint);
+				CompiledItems.Add (p);
 			}
 
 			ValidationId = schema.ValidationId;
 			return errorCount;
 		}
 
-		internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
-			ValidationEventHandler h, XmlSchema schema)
+		internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
+			ValidationEventHandler h, XmlSchema schema, bool raiseError)
 		{
 			if (this == baseParticle) // quick check
-				return;
+				return true;
 
 			XmlSchemaElement el = baseParticle as XmlSchemaElement;
 			if (el != null) {
 				// Forbidden
-				error (h, "Invalid sequence paricle derivation.");
-				return;
+				if (raiseError)
+					error (h, "Invalid sequence paricle derivation.");
+				return false;
 			}
 
 			XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
 			if (seq != null) {
 				// Recurse
-				ValidateOccurenceRangeOK (seq, h, schema);
+				if (!ValidateOccurenceRangeOK (seq, h, schema, raiseError))
+					return false;
 
 				// If it is totally optional, then ignore their contents.
 				if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
 					this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
-					return;
-				this.ValidateRecurse (seq, h, schema);
-				return;
+					return true;
+				return ValidateRecurse (seq, h, schema, raiseError);
 			} 
 
 			XmlSchemaAll all = baseParticle as XmlSchemaAll;
-			XmlSchemaAny any = baseParticle as XmlSchemaAny;
-			XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
 			if (all != null) {
 				// RecurseUnordered
 				XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
 				for (int i = 0; i < this.Items.Count; i++) {
 					XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
 					if (de == null) {
-						error (h, "Invalid sequence particle derivation by restriction from all.");
-						continue;
+						if (raiseError)
+							error (h, "Invalid sequence particle derivation by restriction from all.");
+						return false;
 					}
 					foreach (XmlSchemaElement e in all.Items) {
 						if (e.QualifiedName == de.QualifiedName) {
-							if (already.Contains (e))
-								error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
-							else {
+							if (already.Contains (e)) {
+								if (raiseError)
+									error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
+								return false;
+							} else {
 								already.Add (e);
-								de.ValidateDerivationByRestriction (e, h, schema);
+								if (!de.ValidateDerivationByRestriction (e, h, schema, raiseError))
+									return false;
 							}
 						}
 					}
 				}
 				foreach (XmlSchemaElement e in all.Items)
 					if (!already.Contains (e))
-						if (!e.ValidateIsEmptiable ())
-							error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
-			} else if (any != null) {
+						if (!e.ValidateIsEmptiable ()) {
+							if (raiseError)
+								error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
+							return false;
+						}
+				return true;
+			}
+			XmlSchemaAny any = baseParticle as XmlSchemaAny;
+			if (any != null) {
 				// NSRecurseCheckCardinality
-				ValidateNSRecurseCheckCardinality (any, h, schema);
-				return;
-			} else if (choice != null) {
+				return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
+			}
+			XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
+			if (choice != null) {
 				// MapAndSum
-				// In fact it is not Recurse, but it looks common.
-				ValidateRecurse (choice, h, schema);
+				// In fact it is not Recurse, but it looks almost common.
+				return ValidateSeqRecurseMapSumCommon (choice, h, schema, false, true, raiseError);
 			}
+			return true;
 		}
 
 		internal override decimal GetMinEffectiveTotalRange ()

+ 0 - 3
mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleContent.cs

@@ -18,7 +18,6 @@ namespace System.Xml.Schema
 	{
 		private XmlSchemaContent content;
 		private static string xmlname = "simpleContent";
-		internal object actualBaseSchemaType;
 		public XmlSchemaSimpleContent()
 		{
 		}
@@ -34,7 +33,6 @@ namespace System.Xml.Schema
 		///<remarks>
 		/// 1. Content must be present and one of restriction or extention
 		///</remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
@@ -66,7 +64,6 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 		
-		[MonoTODO]
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.ValidationId))

+ 0 - 13
mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleType.cs

@@ -46,19 +46,6 @@ namespace System.Xml.Schema
 			get{ return variety; }
 		}
 
-		new internal XmlSchemaDatatype Datatype
-		{
-			get {
-				XmlSchemaDatatype dt = BaseSchemaType as XmlSchemaDatatype;
-				if (dt == null) {
-					XmlSchemaSimpleType baseSimple = BaseSchemaType as XmlSchemaSimpleType;
-					if (baseSimple != null)
-						dt = baseSimple.Datatype;
-				}
-				return dt;
-			}
-		}
-
 		/// <remarks>
 		/// For a simple Type:
 		///		1. Content must be present

+ 1 - 3
mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleTypeList.cs

@@ -50,7 +50,6 @@ namespace System.Xml.Schema
 		/// 1. One of itemType or a <simpleType> must be present, but not both.
 		/// 2. id must be of type ID
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			// If this is already compiled this time, simply skip.
@@ -76,14 +75,13 @@ namespace System.Xml.Schema
 			return errorCount;
 		}
 		
-		[MonoTODO]
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			if (IsValidated (schema.ValidationId))
 				return errorCount;
 
 			// As far as I saw, MS.NET handles simpleType.BaseSchemaType as anySimpleType.
-			this.actualBaseSchemaType = XmlSchemaSimpleType.AnySimpleType;
+//			this.actualBaseSchemaType = XmlSchemaSimpleType.AnySimpleType;
 
 			// ListItemType
 			XmlSchemaSimpleType type = itemType;

+ 11 - 12
mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleTypeRestriction.cs

@@ -32,10 +32,9 @@ namespace System.Xml.Schema
 		private object minInclusiveFacet ;
 		private object minExclusiveFacet ;
 		private XmlSchemaFacet.Facet fixedFacets = XmlSchemaFacet.Facet.None; 
-		
-		
 		private static NumberStyles lengthStyle = NumberStyles.Integer;
 
+
 		public XmlSchemaSimpleTypeRestriction()
 		{
 			baseTypeName = XmlQualifiedName.Empty;
@@ -117,16 +116,16 @@ namespace System.Xml.Schema
 		private bool IsAllowedFacet(XmlSchemaFacet xsf) {
 		/* Must be called after this.ValidateActualType, as it uses actualBaseSchemaType */
 
-			XsdAnySimpleType ast = actualBaseSchemaType as XsdAnySimpleType;
+			XsdAnySimpleType ast = ActualBaseSchemaType as XsdAnySimpleType;
 			if (ast != null) {
 				// Based directly on an xsd type 
 				return ast.AllowsFacet(xsf);
 			}
 			else {
-			 XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)actualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
+			 XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
 			 if (st != null) {
 				 XmlSchemaSimpleTypeRestriction str = st as XmlSchemaSimpleTypeRestriction;
-				 if (str != null) {
+				 if (str != null && str != this) {
 					 return str.IsAllowedFacet(xsf);
 				 }
 				 XmlSchemaSimpleTypeList stl = st as XmlSchemaSimpleTypeList;
@@ -166,8 +165,8 @@ namespace System.Xml.Schema
 			
 			XmlSchemaSimpleTypeRestriction baseSTR = null; 
 
-			if (actualBaseSchemaType is XmlSchemaSimpleType) {
-				XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)actualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
+			if (ActualBaseSchemaType is XmlSchemaSimpleType) {
+				XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
 				baseSTR = st as XmlSchemaSimpleTypeRestriction;
 			}
 			
@@ -457,12 +456,12 @@ namespace System.Xml.Schema
 
 
 		private XsdAnySimpleType getDatatype() {
-			XsdAnySimpleType ast = actualBaseSchemaType as XsdAnySimpleType;
+			XsdAnySimpleType ast = ActualBaseSchemaType as XsdAnySimpleType;
 			if (ast != null) {
 				// Based directly on an xsd type 
 				return ast;
 			}
-			XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)actualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
+			XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
 			
 			if (st is XmlSchemaSimpleTypeRestriction) {
 				return ((XmlSchemaSimpleTypeRestriction)st).getDatatype();
@@ -485,11 +484,11 @@ namespace System.Xml.Schema
 					 * that use the nametable and nsmgr are ones that 
 					 * we don't need to parse here.
 					 */ 
-					ret = dt.ParseValue(value, null, null);
+					ret = dt.ParseValue (value, null, null);
 				//	Console.WriteLine("Ret: " + ret);
 					// If we are based on something with facets, check that we are valid
-					if (actualBaseSchemaType is XmlSchemaSimpleType) {
-						XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)actualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
+					if (ActualBaseSchemaType is XmlSchemaSimpleType) {
+						XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType) ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
 						if (st is XmlSchemaSimpleTypeRestriction) {
 							if (((XmlSchemaSimpleTypeRestriction)st).ValidateValueWithFacets(value, null)) {
 								return ret;

+ 1 - 1
mcs/class/System.XML/System.Xml.Schema/XmlSchemaType.cs

@@ -142,7 +142,7 @@ namespace System.Xml.Schema
 			if (recursed)
 				return (this != XmlSchemaComplexType.AnyType);
 			recursed = true;
-			XmlSchemaType baseType = this.BaseSchemaType as XmlSchemaType;
+			XmlSchemaType baseType = this.BaseXmlSchemaType as XmlSchemaType;
 			bool result = false;
 			if (baseType != null)
 				result = baseType.ValidateRecursionCheck ();

+ 0 - 2
mcs/class/System.XML/System.Xml.Schema/XmlSchemaUnique.cs

@@ -19,13 +19,11 @@ namespace System.Xml.Schema
 		/// 1. name must be present
 		/// 2. selector and field must be present
 		/// </remarks>
-		[MonoTODO]
 		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
 		{
 			return base.Compile(h,schema);
 		}
 		
-		[MonoTODO]
 		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
 		{
 			return errorCount;

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

@@ -82,11 +82,11 @@ namespace System.Xml.Schema
 			return true;
 		}
 
-		public static bool CheckLanguage(string lang)
-		{
+//		public static bool CheckLanguage(string lang)
+//		{
 			//check if the string conforms to http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/datatypes.html#language
-			return true;
-		}
+//			return true;
+//		}
 		public static bool CheckNCName(string name)
 		{
 			//check if the string conforms to http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/datatypes.html#NCName