diff --git a/src/xercesc/internal/IGXMLScanner.cpp b/src/xercesc/internal/IGXMLScanner.cpp
index fe7af697b9d2db28cb7500c1aa44efe78d5a53a1..17b8c4603692366938d03fc9eeebca94a3dba13d 100644
--- a/src/xercesc/internal/IGXMLScanner.cpp
+++ b/src/xercesc/internal/IGXMLScanner.cpp
@@ -118,6 +118,7 @@ IGXMLScanner::IGXMLScanner( XMLValidator* const  valToAdopt
     , fPSVIAttrList(0)
     , fModel(0)
     , fPSVIElement(0)
+    , fErrorStack(0)
 {
     try
     {
@@ -167,6 +168,7 @@ IGXMLScanner::IGXMLScanner( XMLDocumentHandler* const docHandler
     , fPSVIAttrList(0)
     , fModel(0)
     , fPSVIElement(0)
+    , fErrorStack(0)
 {
     try
     {	
@@ -604,6 +606,7 @@ void IGXMLScanner::cleanUp()
     delete fUndeclaredAttrRegistryNS;
     delete fPSVIAttrList;
     delete fPSVIElement;
+    delete fErrorStack;
 }
 
 // ---------------------------------------------------------------------------
@@ -1055,6 +1058,20 @@ void IGXMLScanner::scanEndTag(bool& gotData)
         );
     }
 
+    if (fPSVIHandler && fGrammarType == Grammar::SchemaGrammarType)
+    {
+        if (fValidate && topElem->fThisElement->isDeclared())
+        {
+            fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
+            fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
+        }
+        else
+        {
+            fPSVIElemContext.fCurrentDV = 0;
+            fPSVIElemContext.fCurrentTypeInfo = 0;
+        }
+    }
+
     //  If validation is enabled, then lets pass him the list of children and
     //  this element and let him validate it.
     DatatypeValidator* psviMemberType = 0;
@@ -2125,12 +2142,26 @@ bool IGXMLScanner::scanStartTagNS(bool& gotData)
     int currentScope = Grammar::TOP_LEVEL_SCOPE;
     bool laxThisOne = false;
 
-    if (!isRoot && fGrammarType == Grammar::SchemaGrammarType) {
-        // schema validator will have correct type
-        ComplexTypeInfo *currType = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
-        SchemaElementDecl::ModelTypes modelType = (currType)
-                ? ((SchemaElementDecl::ModelTypes)currType->getContentType())
-                : ((SchemaElementDecl*) fElemStack.topElement()->fThisElement)->getModelType();
+    if (!isRoot && fGrammarType == Grammar::SchemaGrammarType)
+    {
+        // schema validator will have correct type if validating
+        SchemaElementDecl* tempElement = (SchemaElementDecl*)
+            fElemStack.topElement()->fThisElement;
+        SchemaElementDecl::ModelTypes modelType = tempElement->getModelType();
+        ComplexTypeInfo *currType = 0;
+
+        if (fValidate)
+        {
+            currType = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
+            if (currType)
+                modelType = (SchemaElementDecl::ModelTypes)currType->getContentType();
+            else // something must have gone wrong
+                modelType = SchemaElementDecl::Any;
+        }
+        else
+        {
+            currType = tempElement->getComplexTypeInfo();
+        }
 
         if ((modelType == SchemaElementDecl::Mixed_Simple)
           ||  (modelType == SchemaElementDecl::Mixed_Complex)
@@ -2582,31 +2613,18 @@ bool IGXMLScanner::scanStartTagNS(bool& gotData)
 
     if (fGrammarType == Grammar::SchemaGrammarType && fPSVIHandler)
     {
-        fPSVIElemContext.fPreviousError = fPSVIElemContext.fErrorOccurred;
+        fErrorStack->push(fPSVIElemContext.fErrorOccurred);
         fPSVIElemContext.fErrorOccurred = false;
         fPSVIElemContext.fElemDepth++;
-        fPSVIElemContext.fValidationRoot = fRootElemName;
-
-        // store current type info so we can restore it later
-        fPSVIElemContext.fPreviousDV = fPSVIElemContext.fCurrentDV;
-        fPSVIElemContext.fPreviousTypeInfo = fPSVIElemContext.fCurrentTypeInfo;
 
         if (elemDecl->isDeclared())
         {
             fPSVIElemContext.fNoneValidationDepth = fPSVIElemContext.fElemDepth;
-
-            // update current type info
-            fPSVIElemContext.fCurrentDV = ((SchemaElementDecl*) elemDecl)->getDatatypeValidator();
-            fPSVIElemContext.fCurrentTypeInfo = ((SchemaElementDecl*) elemDecl)->getComplexTypeInfo();
         }
         else
         {
             fPSVIElemContext.fFullValidationDepth = fPSVIElemContext.fElemDepth;
 
-            // update current type info
-            fPSVIElemContext.fCurrentDV = 0;
-            fPSVIElemContext.fCurrentTypeInfo = 0;
-
             if (isRoot && fValidate)
                 fPSVIElemContext.fErrorOccurred = true;
         }
@@ -2620,27 +2638,15 @@ bool IGXMLScanner::scanStartTagNS(bool& gotData)
         {
             if (((SchemaValidator*) fValidator)->getErrorOccurred())
                 fPSVIElemContext.fErrorOccurred = true;
-
-            // store current type info so we can restore it later
-            fPSVIElemContext.fPreviousDV = fPSVIElemContext.fCurrentDV;
-            fPSVIElemContext.fPreviousTypeInfo = fPSVIElemContext.fCurrentTypeInfo;
-
-            if (elemDecl->isDeclared())
-            {
-                fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
-                fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
-            }
-            else
-            {
-                fPSVIElemContext.fCurrentDV = 0;
-                fPSVIElemContext.fCurrentTypeInfo = 0;
-            }
         }
     }
 
     if (fGrammarType == Grammar::SchemaGrammarType) {
 
-        ComplexTypeInfo* typeinfo = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
+        ComplexTypeInfo* typeinfo = (fValidate)
+            ? ((SchemaValidator*)fValidator)->getCurrentTypeInfo()
+            : ((SchemaElementDecl*) elemDecl)->getComplexTypeInfo();
+
         if (typeinfo) {
             currentScope = typeinfo->getScopeDefined();
 
@@ -2788,6 +2794,21 @@ bool IGXMLScanner::scanStartTagNS(bool& gotData)
         // Pop the element stack back off since it'll never be used now
         fElemStack.popTop();
 
+        // reset current type info
+        if (fPSVIHandler && fGrammarType == Grammar::SchemaGrammarType)
+        {
+            if (fValidate && elemDecl->isDeclared())
+            {
+                fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
+                fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
+            }
+            else
+            {
+                fPSVIElemContext.fCurrentDV = 0;
+                fPSVIElemContext.fCurrentTypeInfo = 0;
+            }
+        }
+
         DatatypeValidator* psviMemberType = 0;
         // If validating, then insure that its legal to have no content
         if (fValidate)
@@ -3332,7 +3353,7 @@ void IGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
     (
         validity
         , validationAttempted
-        , fPSVIElemContext.fValidationRoot
+        , fRootElemName
         , fPSVIElemContext.fIsSpecified
         , (elemDecl->isDeclared()) 
             ? (XSElementDeclaration*) fModel->getXSObject(elemDecl) : 0
@@ -3340,6 +3361,8 @@ void IGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
         , (memberDV) ? (XSSimpleTypeDefinition*) fModel->getXSObject(memberDV) : 0
         , fModel
         , elemDecl->getDefaultValue()
+        , 0
+        , (fPSVIElemContext.fCurrentDV) ? fPSVIElemContext.fCurrentDV->getCanonicalRepresentation() : 0
     );
 
     fPSVIHandler->handleElementPSVI
@@ -3352,13 +3375,9 @@ void IGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
     // decrease element depth
     fPSVIElemContext.fElemDepth--;
 
-    // restore type info
-    fPSVIElemContext.fCurrentDV = fPSVIElemContext.fPreviousDV;
-    fPSVIElemContext.fCurrentTypeInfo = fPSVIElemContext.fPreviousTypeInfo;
-
     // reset error occurred
     fPSVIElemContext.fErrorOccurred = 
-        fPSVIElemContext.fErrorOccurred && fPSVIElemContext.fPreviousError;
+        fPSVIElemContext.fErrorOccurred && fErrorStack->pop();
 }
 
 void IGXMLScanner::resetPSVIElemContext()
@@ -3368,7 +3387,6 @@ void IGXMLScanner::resetPSVIElemContext()
     fPSVIElemContext.fElemDepth = -1;
     fPSVIElemContext.fFullValidationDepth = -1;
     fPSVIElemContext.fNoneValidationDepth = -1;
-    fPSVIElemContext.fValidationRoot = 0;
     fPSVIElemContext.fCurrentDV = 0;
     fPSVIElemContext.fCurrentTypeInfo = 0;
 }
diff --git a/src/xercesc/internal/IGXMLScanner.hpp b/src/xercesc/internal/IGXMLScanner.hpp
index a685300d71209e4043d91a8711cc215b95d87755..c18ea971650ce2f4ad837981ef2ddf446a13a9c8 100644
--- a/src/xercesc/internal/IGXMLScanner.hpp
+++ b/src/xercesc/internal/IGXMLScanner.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.17  2003/11/28 19:54:31  knoaman
+ * PSVIElement update
+ *
  * Revision 1.16  2003/11/28 05:13:29  neilg
  * Fix state-ful duplicate attribute detection when the integrated
  * scanner is in use and namespaces are off.  Also, implement
@@ -391,6 +394,7 @@ private :
     PSVIAttributeList *                     fPSVIAttrList;
     XSModel*                                fModel;
     PSVIElement*                            fPSVIElement;
+    ValueStackOf<bool>*                     fErrorStack;
     PSVIElemContext                         fPSVIElemContext;
 };
 
diff --git a/src/xercesc/internal/IGXMLScanner2.cpp b/src/xercesc/internal/IGXMLScanner2.cpp
index 9b1a22adee1d0489016c50f44e22be6c8319f7ed..ae8d67e781a5df3e758e14757b93ad79429d21be 100644
--- a/src/xercesc/internal/IGXMLScanner2.cpp
+++ b/src/xercesc/internal/IGXMLScanner2.cpp
@@ -1248,6 +1248,9 @@ void IGXMLScanner::scanReset(const InputSource& src)
         if (!fPSVIElement)
             fPSVIElement = new (fMemoryManager) PSVIElement(fMemoryManager);
 
+        if (!fErrorStack)
+            fErrorStack = new (fMemoryManager) ValueStackOf<bool>(8, fMemoryManager);
+
         resetPSVIElemContext();
     }
 
diff --git a/src/xercesc/internal/SGXMLScanner.cpp b/src/xercesc/internal/SGXMLScanner.cpp
index 22ea52c7c1d8cd54cb4178755a28a0f468185e26..82fdadd9b379733d28b220287f2c7e398223c930 100644
--- a/src/xercesc/internal/SGXMLScanner.cpp
+++ b/src/xercesc/internal/SGXMLScanner.cpp
@@ -119,6 +119,7 @@ SGXMLScanner::SGXMLScanner( XMLValidator* const valToAdopt
     , fPSVIAttrList(0)
     , fModel(0)
     , fPSVIElement(0)
+    , fErrorStack(0)
 {
     try
     {
@@ -171,6 +172,7 @@ SGXMLScanner::SGXMLScanner( XMLDocumentHandler* const docHandler
     , fPSVIAttrList(0)
     , fModel(0)
     , fPSVIElement(0)
+    , fErrorStack(0)
 {
     try
     {	
@@ -966,6 +968,20 @@ void SGXMLScanner::scanEndTag(bool& gotData)
         );
     }
 
+    if (fPSVIHandler)
+    {
+        if (fValidate && topElem->fThisElement->isDeclared())
+        {
+            fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
+            fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
+        }
+        else
+        {
+            fPSVIElemContext.fCurrentDV = 0;
+            fPSVIElemContext.fCurrentTypeInfo = 0;
+        }
+    }
+
     //  If validation is enabled, then lets pass him the list of children and
     //  this element and let him validate it.
     DatatypeValidator* psviMemberType = 0;
@@ -1183,12 +1199,26 @@ bool SGXMLScanner::scanStartTag(bool& gotData)
     XMLContentModel* cm = 0;
     int currentScope = Grammar::TOP_LEVEL_SCOPE;
     bool laxThisOne = false;
-    if (!isRoot) {
-        // schema validator will have correct type
-        ComplexTypeInfo *currType = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
-        SchemaElementDecl::ModelTypes modelType = (currType)
-                ? ((SchemaElementDecl::ModelTypes)currType->getContentType())
-                : ((SchemaElementDecl*) fElemStack.topElement()->fThisElement)->getModelType();
+    if (!isRoot)
+    {
+        // schema validator will have correct type if validating
+        SchemaElementDecl* tempElement = (SchemaElementDecl*)
+            fElemStack.topElement()->fThisElement;
+        SchemaElementDecl::ModelTypes modelType = tempElement->getModelType();
+        ComplexTypeInfo *currType = 0;
+
+        if (fValidate)
+        {
+            currType = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
+            if (currType)
+                modelType = (SchemaElementDecl::ModelTypes)currType->getContentType();
+            else // something must have gone wrong
+                modelType = SchemaElementDecl::Any;
+        }
+        else
+        {
+            currType = tempElement->getComplexTypeInfo();
+        }
 
         if ((modelType == SchemaElementDecl::Mixed_Simple)
           ||  (modelType == SchemaElementDecl::Mixed_Complex)
@@ -1550,31 +1580,18 @@ bool SGXMLScanner::scanStartTag(bool& gotData)
 
     if (fPSVIHandler)
     {
-        fPSVIElemContext.fPreviousError = fPSVIElemContext.fErrorOccurred;
+        fErrorStack->push(fPSVIElemContext.fErrorOccurred);
         fPSVIElemContext.fErrorOccurred = false;
         fPSVIElemContext.fElemDepth++;
-        fPSVIElemContext.fValidationRoot = fRootElemName;
-
-        // store current type info so we can restore it later
-        fPSVIElemContext.fPreviousDV = fPSVIElemContext.fCurrentDV;
-        fPSVIElemContext.fPreviousTypeInfo = fPSVIElemContext.fCurrentTypeInfo;
 
         if (elemDecl->isDeclared())
         {
             fPSVIElemContext.fNoneValidationDepth = fPSVIElemContext.fElemDepth;
-
-            // update current type info
-            fPSVIElemContext.fCurrentDV = ((SchemaElementDecl*) elemDecl)->getDatatypeValidator();
-            fPSVIElemContext.fCurrentTypeInfo = ((SchemaElementDecl*) elemDecl)->getComplexTypeInfo();
         }
         else
         {
             fPSVIElemContext.fFullValidationDepth = fPSVIElemContext.fElemDepth;
 
-            // update current type info
-            fPSVIElemContext.fCurrentDV = 0;
-            fPSVIElemContext.fCurrentTypeInfo = 0;
-
             if (isRoot && fValidate)
                 fPSVIElemContext.fErrorOccurred = true;
         }
@@ -1588,25 +1605,13 @@ bool SGXMLScanner::scanStartTag(bool& gotData)
         {
             if (((SchemaValidator*) fValidator)->getErrorOccurred())
                 fPSVIElemContext.fErrorOccurred = true;
-
-            // store current type info so we can restore it later
-            fPSVIElemContext.fPreviousDV = fPSVIElemContext.fCurrentDV;
-            fPSVIElemContext.fPreviousTypeInfo = fPSVIElemContext.fCurrentTypeInfo;
-
-            if (elemDecl->isDeclared())
-            {
-                fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
-                fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
-            }
-            else
-            {
-                fPSVIElemContext.fCurrentDV = 0;
-                fPSVIElemContext.fCurrentTypeInfo = 0;
-            }
         }
     }
 
-    ComplexTypeInfo* typeinfo = ((SchemaValidator*)fValidator)->getCurrentTypeInfo();
+    ComplexTypeInfo* typeinfo = (fValidate)
+        ? ((SchemaValidator*)fValidator)->getCurrentTypeInfo()
+        : ((SchemaElementDecl*) elemDecl)->getComplexTypeInfo();
+
     if (typeinfo) {
         currentScope = typeinfo->getScopeDefined();
 
@@ -1742,6 +1747,21 @@ bool SGXMLScanner::scanStartTag(bool& gotData)
         // Pop the element stack back off since it'll never be used now
         fElemStack.popTop();
 
+        // reset current type info
+        if (fPSVIHandler && fGrammarType == Grammar::SchemaGrammarType)
+        {
+            if (fValidate && elemDecl->isDeclared())
+            {
+                fPSVIElemContext.fCurrentDV = ((SchemaValidator*) fValidator)->getCurrentDatatypeValidator();
+                fPSVIElemContext.fCurrentTypeInfo = ((SchemaValidator*) fValidator)->getCurrentTypeInfo();
+            }
+            else
+            {
+                fPSVIElemContext.fCurrentDV = 0;
+                fPSVIElemContext.fCurrentTypeInfo = 0;
+            }
+        }
+
         // If validating, then insure that its legal to have no content
         DatatypeValidator* psviMemberType = 0;
         if (fValidate)
@@ -2112,6 +2132,7 @@ void SGXMLScanner::cleanUp()
     delete fUndeclaredAttrRegistryNS;
     delete fPSVIAttrList;
     delete fPSVIElement;
+    delete fErrorStack;
 }
 
 void SGXMLScanner::resizeElemState() {
@@ -3173,6 +3194,9 @@ void SGXMLScanner::scanReset(const InputSource& src)
         if (!fPSVIElement)
             fPSVIElement = new (fMemoryManager) PSVIElement(fMemoryManager);
 
+        if (!fErrorStack)
+            fErrorStack = new (fMemoryManager) ValueStackOf<bool>(8, fMemoryManager);
+
         resetPSVIElemContext();
     }
 
@@ -4758,7 +4782,7 @@ void SGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
     (
         validity
         , validationAttempted
-        , fPSVIElemContext.fValidationRoot
+        , fRootElemName
         , fPSVIElemContext.fIsSpecified
         , (elemDecl->isDeclared()) 
             ? (XSElementDeclaration*) fModel->getXSObject(elemDecl) : 0
@@ -4766,6 +4790,8 @@ void SGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
         , (memberDV) ? (XSSimpleTypeDefinition*) fModel->getXSObject(memberDV) : 0
         , fModel
         , elemDecl->getDefaultValue()
+        , 0
+        , (fPSVIElemContext.fCurrentDV) ? fPSVIElemContext.fCurrentDV->getCanonicalRepresentation() : 0
     );
 
     fPSVIHandler->handleElementPSVI
@@ -4778,13 +4804,9 @@ void SGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
     // decrease element depth
     fPSVIElemContext.fElemDepth--;
 
-    // restore type info
-    fPSVIElemContext.fCurrentDV = fPSVIElemContext.fPreviousDV;
-    fPSVIElemContext.fCurrentTypeInfo = fPSVIElemContext.fPreviousTypeInfo;
-
     // reset error occurred
     fPSVIElemContext.fErrorOccurred = 
-        fPSVIElemContext.fErrorOccurred && fPSVIElemContext.fPreviousError;
+        fPSVIElemContext.fErrorOccurred && fErrorStack->pop();
 }
 
 void SGXMLScanner::resetPSVIElemContext()
@@ -4794,7 +4816,6 @@ void SGXMLScanner::resetPSVIElemContext()
     fPSVIElemContext.fElemDepth = -1;
     fPSVIElemContext.fFullValidationDepth = -1;
     fPSVIElemContext.fNoneValidationDepth = -1;
-    fPSVIElemContext.fValidationRoot = 0;
     fPSVIElemContext.fCurrentDV = 0;
     fPSVIElemContext.fCurrentTypeInfo = 0;
 }
diff --git a/src/xercesc/internal/SGXMLScanner.hpp b/src/xercesc/internal/SGXMLScanner.hpp
index 91cc41722b572c0471984467f6f403a3e8841003..a43c01f585c0a380131e5528f92ba18ccdbbcb80 100644
--- a/src/xercesc/internal/SGXMLScanner.hpp
+++ b/src/xercesc/internal/SGXMLScanner.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.15  2003/11/28 19:54:31  knoaman
+ * PSVIElement update
+ *
  * Revision 1.14  2003/11/27 22:52:37  knoaman
  * PSVIElement implementation
  *
@@ -367,6 +370,7 @@ private :
     PSVIAttributeList *                     fPSVIAttrList;
     XSModel*                                fModel;
     PSVIElement*                            fPSVIElement;
+    ValueStackOf<bool>*                     fErrorStack;
     PSVIElemContext                         fPSVIElemContext;
 };
 
diff --git a/src/xercesc/internal/XMLScanner.hpp b/src/xercesc/internal/XMLScanner.hpp
index d4fd032e65989e1edc3b9d967c304556b6761168..d518ce12fb75c7e83f2e51b1133b651dfe833fa7 100644
--- a/src/xercesc/internal/XMLScanner.hpp
+++ b/src/xercesc/internal/XMLScanner.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.30  2003/11/28 19:54:31  knoaman
+ * PSVIElement update
+ *
  * Revision 1.29  2003/11/27 22:52:37  knoaman
  * PSVIElement implementation
  *
@@ -328,15 +331,11 @@ struct PSVIElemContext
 {
     bool               fIsSpecified;
     bool               fErrorOccurred;
-    bool               fPreviousError;
     int                fElemDepth;
     int                fFullValidationDepth;
     int                fNoneValidationDepth;
-    const XMLCh*       fValidationRoot;
     DatatypeValidator* fCurrentDV;
-    DatatypeValidator* fPreviousDV;
     ComplexTypeInfo*   fCurrentTypeInfo;
-    ComplexTypeInfo*   fPreviousTypeInfo;
 };
 
 //  This is the mondo scanner class, which does the vast majority of the