diff --git a/src/xercesc/NLS/EN_US/XMLErrList_EN_US.Xml b/src/xercesc/NLS/EN_US/XMLErrList_EN_US.Xml
index e9633b6be008ed91a6f93294ffef652dbf38b809..e518eaddbf2acfc23718670bb239b693a25d6ff7 100644
--- a/src/xercesc/NLS/EN_US/XMLErrList_EN_US.Xml
+++ b/src/xercesc/NLS/EN_US/XMLErrList_EN_US.Xml
@@ -405,7 +405,8 @@
             <Message Id="NoUseAbstractType" Text="type of element '{0}' is abstract; use 'xsi:type' to specify non-abstract type instead"/>
             <Message Id="BadXsiType" Text="type '{0}' specified in 'xsi:type' cannot be resolved"/>
             <Message Id="NonDerivedXsiType" Text="type '{0}' specified in 'xsi:type' does not derive from type of element '{1}'"/>
-            <Message Id="NoSubforBlock" Text="element '{0}' does not permit substitution"/>
+            <Message Id="ElemNoSubforBlock" Text="element '{0}' does not permit substitution"/>
+            <Message Id="TypeNoSubforBlock" Text="complex type '{0}' does not permit substitution"/>
             <Message Id="AttributeNotQualified" Text="attribute '{0}' must be qualified"/>
             <Message Id="AttributeNotUnQualified" Text="attribute '{0}' must be unqualified"/>
             <Message Id="IC_FieldMultipleMatch" Text="identity constraint field matches more than one value within the scope of its selector; field must match unique value"/>
diff --git a/src/xercesc/framework/XMLValidityCodes.hpp b/src/xercesc/framework/XMLValidityCodes.hpp
index 82a96e5b694afe7b43735b971fe918e05238d959..aed00fbee399b537a975072a3ad050c36777e14c 100644
--- a/src/xercesc/framework/XMLValidityCodes.hpp
+++ b/src/xercesc/framework/XMLValidityCodes.hpp
@@ -74,29 +74,30 @@ public :
       , NoUseAbstractType                  = 57
       , BadXsiType                         = 58
       , NonDerivedXsiType                  = 59
-      , NoSubforBlock                      = 60
-      , AttributeNotQualified              = 61
-      , AttributeNotUnQualified            = 62
-      , IC_FieldMultipleMatch              = 63
-      , IC_UnknownField                    = 64
-      , IC_AbsentKeyValue                  = 65
-      , IC_KeyNotEnoughValues              = 66
-      , IC_KeyMatchesNillable              = 67
-      , IC_DuplicateUnique                 = 68
-      , IC_DuplicateKey                    = 69
-      , IC_KeyRefOutOfScope                = 70
-      , IC_KeyNotFound                     = 71
-      , NonWSContent                       = 72
-      , EmptyElemNotationAttr              = 73
-      , EmptyElemHasContent                = 74
-      , ElemOneNotationAttr                = 75
-      , AttrDupToken                       = 76
-      , ElemChildrenHasInvalidWS           = 77
-      , E_HighBounds                       = 78
-      , W_LowBounds                        = 79
-      , W_HighBounds                       = 80
-      , F_LowBounds                        = 81
-      , F_HighBounds                       = 82
+      , ElemNoSubforBlock                  = 60
+      , TypeNoSubforBlock                  = 61
+      , AttributeNotQualified              = 62
+      , AttributeNotUnQualified            = 63
+      , IC_FieldMultipleMatch              = 64
+      , IC_UnknownField                    = 65
+      , IC_AbsentKeyValue                  = 66
+      , IC_KeyNotEnoughValues              = 67
+      , IC_KeyMatchesNillable              = 68
+      , IC_DuplicateUnique                 = 69
+      , IC_DuplicateKey                    = 70
+      , IC_KeyRefOutOfScope                = 71
+      , IC_KeyNotFound                     = 72
+      , NonWSContent                       = 73
+      , EmptyElemNotationAttr              = 74
+      , EmptyElemHasContent                = 75
+      , ElemOneNotationAttr                = 76
+      , AttrDupToken                       = 77
+      , ElemChildrenHasInvalidWS           = 78
+      , E_HighBounds                       = 79
+      , W_LowBounds                        = 80
+      , W_HighBounds                       = 81
+      , F_LowBounds                        = 82
+      , F_HighBounds                       = 83
     };
 
     static bool isFatal(const XMLValid::Codes toCheck)
diff --git a/src/xercesc/util/MsgLoaders/ICU/resources/root.txt b/src/xercesc/util/MsgLoaders/ICU/resources/root.txt
index cc34cccd190737da1966289f1cd46d050fb3ab4d..3ccca5b684dac3d17f52e80e5544c2db476b50cd 100644
--- a/src/xercesc/util/MsgLoaders/ICU/resources/root.txt
+++ b/src/xercesc/util/MsgLoaders/ICU/resources/root.txt
@@ -354,6 +354,7 @@ root {
 		"type '{0}' specified in 'xsi:type' cannot be resolved" ,
 		"type '{0}' specified in 'xsi:type' does not derive from type of element '{1}'" ,
 		"element '{0}' does not permit substitution" ,
+		"complex type '{0}' does not permit substitution" ,
 		"attribute '{0}' must be qualified" ,
 		"attribute '{0}' must be unqualified" ,
 		"identity constraint field matches more than one value within the scope of its selector; field must match unique value" ,
diff --git a/src/xercesc/util/MsgLoaders/InMemory/XercesMessages_en_US.hpp b/src/xercesc/util/MsgLoaders/InMemory/XercesMessages_en_US.hpp
index b660b9a2773c0b329303293a0f1a0397fbb05f82..e50951444028bc643ab7a8de9bca23efa07a7e0b 100644
--- a/src/xercesc/util/MsgLoaders/InMemory/XercesMessages_en_US.hpp
+++ b/src/xercesc/util/MsgLoaders/InMemory/XercesMessages_en_US.hpp
@@ -707,6 +707,8 @@ const XMLCh gXMLValidityArray[][128] =
       0x0074,0x0020,0x0027,0x007B,0x0031,0x007D,0x0027,0x00 }
   , { 0x0065,0x006C,0x0065,0x006D,0x0065,0x006E,0x0074,0x0020,0x0027,0x007B,0x0030,0x007D,0x0027,0x0020,0x0064,0x006F,0x0065,0x0073,0x0020,0x006E,0x006F,0x0074,0x0020,0x0070,0x0065,0x0072,0x006D,0x0069,0x0074,0x0020,0x0073,0x0075,0x0062,0x0073,0x0074,
       0x0069,0x0074,0x0075,0x0074,0x0069,0x006F,0x006E,0x00 }
+  , { 0x0063,0x006F,0x006D,0x0070,0x006C,0x0065,0x0078,0x0020,0x0074,0x0079,0x0070,0x0065,0x0020,0x0027,0x007B,0x0030,0x007D,0x0027,0x0020,0x0064,0x006F,0x0065,0x0073,0x0020,0x006E,0x006F,0x0074,0x0020,0x0070,0x0065,0x0072,0x006D,0x0069,0x0074,0x0020,
+      0x0073,0x0075,0x0062,0x0073,0x0074,0x0069,0x0074,0x0075,0x0074,0x0069,0x006F,0x006E,0x00 }
   , { 0x0061,0x0074,0x0074,0x0072,0x0069,0x0062,0x0075,0x0074,0x0065,0x0020,0x0027,0x007B,0x0030,0x007D,0x0027,0x0020,0x006D,0x0075,0x0073,0x0074,0x0020,0x0062,0x0065,0x0020,0x0071,0x0075,0x0061,0x006C,0x0069,0x0066,0x0069,0x0065,0x0064,0x00 }
   , { 0x0061,0x0074,0x0074,0x0072,0x0069,0x0062,0x0075,0x0074,0x0065,0x0020,0x0027,0x007B,0x0030,0x007D,0x0027,0x0020,0x006D,0x0075,0x0073,0x0074,0x0020,0x0062,0x0065,0x0020,0x0075,0x006E,0x0071,0x0075,0x0061,0x006C,0x0069,0x0066,0x0069,0x0065,0x0064,
       0x00 }
@@ -749,7 +751,7 @@ const XMLCh gXMLValidityArray[][128] =
   , { 0x0045,0x005F,0x0045,0x006E,0x0064,0x00 }
 
 };
-const unsigned int gXMLValidityArraySize = 83;
+const unsigned int gXMLValidityArraySize = 84;
 
 const XMLCh gXMLExceptArray[][128] = 
 {
diff --git a/src/xercesc/util/MsgLoaders/MsgCatalog/XercesMessages_en_US.Msg b/src/xercesc/util/MsgLoaders/MsgCatalog/XercesMessages_en_US.Msg
index a89e704dd2e5a820c2b46094ceb54a9741ab6f2d..423db733ee40a608578b3eddd739539d6e31e947 100644
--- a/src/xercesc/util/MsgLoaders/MsgCatalog/XercesMessages_en_US.Msg
+++ b/src/xercesc/util/MsgLoaders/MsgCatalog/XercesMessages_en_US.Msg
@@ -343,23 +343,24 @@ $set 2
 58  type '{0}' specified in 'xsi:type' cannot be resolved
 59  type '{0}' specified in 'xsi:type' does not derive from type of element '{1}'
 60  element '{0}' does not permit substitution
-61  attribute '{0}' must be qualified
-62  attribute '{0}' must be unqualified
-63  identity constraint field matches more than one value within the scope of its selector; field must match unique value
-64  unknown identity constraint field
-65  element '{0}' has identity constraint key with no value
-66  element '{0}' does not have enough values for identity constraint key '{1}'
-67  element '{0}' declares identity constraint key that matches nillable element
-68  element '{0}' declares duplicate identity constraint unique values
-69  element '{0}' declares duplicate identity constraint key values
-70  keyref '{0}' refers to out of scope key/unique
-71  identity constraint key for element '{0}' not found
-72  non-whitespace characters are not allowed in schema declarations other than appinfo and documentation
-73  element '{0}' declared EMPTY but has attribute '{1}' of type NOTATION
-74  element '{0}' declared EMPTY and cannot have content, not even entity references, comments, PIs, or whitespaces
-75  element '{0}' has more than one attribute of type NOTATION
-76  attribute '{0}' has non-distinct token '{1}'
-77  content model of element '{0}' does not allow escaped whitespaces
+61  complex type '{0}' does not permit substitution
+62  attribute '{0}' must be qualified
+63  attribute '{0}' must be unqualified
+64  identity constraint field matches more than one value within the scope of its selector; field must match unique value
+65  unknown identity constraint field
+66  element '{0}' has identity constraint key with no value
+67  element '{0}' does not have enough values for identity constraint key '{1}'
+68  element '{0}' declares identity constraint key that matches nillable element
+69  element '{0}' declares duplicate identity constraint unique values
+70  element '{0}' declares duplicate identity constraint key values
+71  keyref '{0}' refers to out of scope key/unique
+72  identity constraint key for element '{0}' not found
+73  non-whitespace characters are not allowed in schema declarations other than appinfo and documentation
+74  element '{0}' declared EMPTY but has attribute '{1}' of type NOTATION
+75  element '{0}' declared EMPTY and cannot have content, not even entity references, comments, PIs, or whitespaces
+76  element '{0}' has more than one attribute of type NOTATION
+77  attribute '{0}' has non-distinct token '{1}'
+78  content model of element '{0}' does not allow escaped whitespaces
 
 
 $set 3
diff --git a/src/xercesc/util/MsgLoaders/Win32/Version.rc b/src/xercesc/util/MsgLoaders/Win32/Version.rc
index d3b3d715e169459ddbe05410c3f8f6a485ad8076..65c5a4528c80de983b5f724f984d95868f844a0d 100644
--- a/src/xercesc/util/MsgLoaders/Win32/Version.rc
+++ b/src/xercesc/util/MsgLoaders/Win32/Version.rc
@@ -448,23 +448,24 @@ BEGIN
     16442             L"type '{0}' specified in 'xsi:type' cannot be resolved"
     16443             L"type '{0}' specified in 'xsi:type' does not derive from type of element '{1}'"
     16444             L"element '{0}' does not permit substitution"
-    16445             L"attribute '{0}' must be qualified"
-    16446             L"attribute '{0}' must be unqualified"
-    16447             L"identity constraint field matches more than one value within the scope of its selector; field must match unique value"
-    16448             L"unknown identity constraint field"
-    16449             L"element '{0}' has identity constraint key with no value"
-    16450             L"element '{0}' does not have enough values for identity constraint key '{1}'"
-    16451             L"element '{0}' declares identity constraint key that matches nillable element"
-    16452             L"element '{0}' declares duplicate identity constraint unique values"
-    16453             L"element '{0}' declares duplicate identity constraint key values"
-    16454             L"keyref '{0}' refers to out of scope key/unique"
-    16455             L"identity constraint key for element '{0}' not found"
-    16456             L"non-whitespace characters are not allowed in schema declarations other than appinfo and documentation"
-    16457             L"element '{0}' declared EMPTY but has attribute '{1}' of type NOTATION"
-    16458             L"element '{0}' declared EMPTY and cannot have content, not even entity references, comments, PIs, or whitespaces"
-    16459             L"element '{0}' has more than one attribute of type NOTATION"
-    16460             L"attribute '{0}' has non-distinct token '{1}'"
-    16461             L"content model of element '{0}' does not allow escaped whitespaces"
+    16445             L"complex type '{0}' does not permit substitution"
+    16446             L"attribute '{0}' must be qualified"
+    16447             L"attribute '{0}' must be unqualified"
+    16448             L"identity constraint field matches more than one value within the scope of its selector; field must match unique value"
+    16449             L"unknown identity constraint field"
+    16450             L"element '{0}' has identity constraint key with no value"
+    16451             L"element '{0}' does not have enough values for identity constraint key '{1}'"
+    16452             L"element '{0}' declares identity constraint key that matches nillable element"
+    16453             L"element '{0}' declares duplicate identity constraint unique values"
+    16454             L"element '{0}' declares duplicate identity constraint key values"
+    16455             L"keyref '{0}' refers to out of scope key/unique"
+    16456             L"identity constraint key for element '{0}' not found"
+    16457             L"non-whitespace characters are not allowed in schema declarations other than appinfo and documentation"
+    16458             L"element '{0}' declared EMPTY but has attribute '{1}' of type NOTATION"
+    16459             L"element '{0}' declared EMPTY and cannot have content, not even entity references, comments, PIs, or whitespaces"
+    16460             L"element '{0}' has more than one attribute of type NOTATION"
+    16461             L"attribute '{0}' has non-distinct token '{1}'"
+    16462             L"content model of element '{0}' does not allow escaped whitespaces"
 END
 STRINGTABLE DISCARDABLE
 BEGIN
diff --git a/src/xercesc/validators/schema/SchemaValidator.cpp b/src/xercesc/validators/schema/SchemaValidator.cpp
index 672c9b43832dd574dfc9de46f8b8f830bf511a6f..cf8c7b9bf5c03b5c150511e19ba9c9bd02999bee 100644
--- a/src/xercesc/validators/schema/SchemaValidator.cpp
+++ b/src/xercesc/validators/schema/SchemaValidator.cpp
@@ -573,10 +573,11 @@ void SchemaValidator::validateElement(const   XMLElementDecl*  elemDef)
                             emitError(XMLValid::NonDerivedXsiType, fXsiType->getRawName(), elemDef->getFullName());
                             fErrorOccurred = true;
                         }
-                        else {
+                        else if(fCurrentDatatypeValidator != xsiTypeDV) 
+                        {
                             // the type is derived from ancestor
-                            if (((SchemaElementDecl*)elemDef)->getBlockSet() == SchemaSymbols::XSD_RESTRICTION) {
-                                emitError(XMLValid::NoSubforBlock, fXsiType->getRawName(), elemDef->getFullName());
+                            if ((((SchemaElementDecl*)elemDef)->getBlockSet() & SchemaSymbols::XSD_RESTRICTION) != 0) {
+                                emitError(XMLValid::ElemNoSubforBlock, elemDef->getFullName());
                                 fErrorOccurred = true;
                             }
                             if (elemDef->hasAttDefs()) {
@@ -635,10 +636,14 @@ void SchemaValidator::validateElement(const   XMLElementDecl*  elemDef)
                                     emitError(XMLValid::NonDerivedXsiType, fXsiType->getRawName(), elemDef->getFullName());
                                     fErrorOccurred = true;
                                 }
-                                else {
+                                else if(elemTypeInfo != typeInfo) {
                                     int derivationMethod = typeInfo->getDerivedBy();
                                     if ((((SchemaElementDecl*)elemDef)->getBlockSet() & derivationMethod) != 0) {
-                                        emitError(XMLValid::NoSubforBlock, fXsiType->getRawName(), elemDef->getFullName());
+                                        emitError(XMLValid::ElemNoSubforBlock, elemDef->getFullName());
+                                        fErrorOccurred = true;
+                                    }
+                                    if ((tempType->getBlockSet() & derivationMethod) != 0) {
+                                        emitError(XMLValid::TypeNoSubforBlock, tempType->getTypeName());
                                         fErrorOccurred = true;
                                     }
                                 }
@@ -676,10 +681,11 @@ void SchemaValidator::validateElement(const   XMLElementDecl*  elemDef)
                                 emitError(XMLValid::NonDerivedXsiType, fXsiType->getRawName(), elemDef->getFullName());
                                 fErrorOccurred = true;
                             }
-                            else {
+                            else if(fCurrentDatatypeValidator != xsiTypeDV)
+                            {
                                 // the type is derived from ancestor
-                                if (((SchemaElementDecl*)elemDef)->getBlockSet() == SchemaSymbols::XSD_RESTRICTION) {
-                                    emitError(XMLValid::NoSubforBlock, fXsiType->getRawName(), elemDef->getFullName());
+                                if ((((SchemaElementDecl*)elemDef)->getBlockSet() & SchemaSymbols::XSD_RESTRICTION) != 0) {
+                                    emitError(XMLValid::ElemNoSubforBlock, elemDef->getFullName());
                                     fErrorOccurred = true;
                                 }
                                 if (elemDef->hasAttDefs()) {