diff --git a/src/util/ValueVectorOf.c b/src/util/ValueVectorOf.c
index e2475ff3b95b66c1052a0ef707b449462f2071f3..dcaa5c50aa5388a93ce12c0e9c64c6c934921115 100644
--- a/src/util/ValueVectorOf.c
+++ b/src/util/ValueVectorOf.c
@@ -56,6 +56,9 @@
 
 /**
  * $Log$
+ * Revision 1.5  2002/01/10 17:44:49  knoaman
+ * Fix for bug 5786.
+ *
  * Revision 1.4  2001/08/09 15:24:37  knoaman
  * add support for <anyAttribute> declaration.
  *
@@ -207,9 +210,10 @@ template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
 }
 
 template <class TElem> 
-bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck) {
+bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
+                                           const unsigned int startIndex) {
 
-    for (unsigned int i = 0; i < fCurCount; i++) {
+    for (unsigned int i = startIndex; i < fCurCount; i++) {
         if (fElemList[i] == toCheck) {
             return true;
         }
diff --git a/src/util/ValueVectorOf.hpp b/src/util/ValueVectorOf.hpp
index fc0e80a0cdcc7ced6d1e48490e9eb8f212563025..81bf6397a42efeaf1513d55e28921216ccbe7557 100644
--- a/src/util/ValueVectorOf.hpp
+++ b/src/util/ValueVectorOf.hpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.6  2002/01/10 17:44:49  knoaman
+ * Fix for bug 5786.
+ *
  * Revision 1.5  2001/08/09 15:24:37  knoaman
  * add support for <anyAttribute> declaration.
  *
@@ -112,7 +115,7 @@ public :
     void insertElementAt(const TElem& toInsert, const unsigned int insertAt);
     void removeElementAt(const unsigned int removeAt);
     void removeAllElements();
-    bool containsElement(const TElem& toCheck);
+    bool containsElement(const TElem& toCheck, const unsigned int startIndex = 0);
 
 
     // -----------------------------------------------------------------------
diff --git a/src/validators/schema/TraverseSchema.cpp b/src/validators/schema/TraverseSchema.cpp
index 151ac27812038c941fb55f99189bb6dd16ee7023..63320060600b15f7f47a37445ecfe919eb65b03a 100644
--- a/src/validators/schema/TraverseSchema.cpp
+++ b/src/validators/schema/TraverseSchema.cpp
@@ -190,6 +190,7 @@ TraverseSchema::TraverseSchema( const DOM_Element&                 schemaRoot
     , fEmptyNamespaceURI(-1)
     , fCurrentScope(Grammar::TOP_LEVEL_SCOPE)
     , fAnonXSTypeCount(0)
+    , fCircularCheckIndex(0)
     , fFinalDefault(0)
     , fBlockDefault(0)
     , fScopeCount(0)
@@ -1113,6 +1114,7 @@ int TraverseSchema::traverseComplexTypeDecl(const DOM_Element& elem) {
     // Create a new instance
     // -----------------------------------------------------------------------
     ComplexTypeInfo* typeInfo = new ComplexTypeInfo();
+    unsigned int previousCircularCheckIndex = fCircularCheckIndex;
     int previousScope = fCurrentScope;
     fCurrentScope = fScopeCount++;
 
@@ -1210,6 +1212,7 @@ int TraverseSchema::traverseComplexTypeDecl(const DOM_Element& elem) {
     // ------------------------------------------------------------------
     fCurrentScope = previousScope;
     fCurrentComplexType = saveTypeInfo;
+    fCircularCheckIndex = previousCircularCheckIndex;
     resetCurrentTypeNameStack(0);
 
 
@@ -5393,6 +5396,8 @@ void TraverseSchema::processComplexContent(const XMLCh* const typeName,
 
     if (childElem != 0) {
 
+        fCircularCheckIndex = fCurrentTypeNameStack->size();
+
         // --------------------------------------------------------------------
         // GROUP, ALL, SEQUENCE or CHOICE, followed by attributes, if specified.
         // Note that it's possible that only attributes are specified.
@@ -5617,7 +5622,7 @@ void TraverseSchema::processBaseTypeInfo(const XMLCh* const baseName,
 
         // Circular check
         if (baseComplexTypeInfo &&
-            fCurrentTypeNameStack->containsElement(fStringPool->addOrFind(fullBaseName))) {
+            fCurrentTypeNameStack->containsElement(fStringPool->addOrFind(fullBaseName), fCircularCheckIndex)) {
 
             reportSchemaError(XMLUni::fgXMLErrDomain, XMLErrs::NoCircularDefinition, fullBaseName);
             throw TraverseSchema::InvalidComplexTypeInfo;
diff --git a/src/validators/schema/TraverseSchema.hpp b/src/validators/schema/TraverseSchema.hpp
index f0528b9667a8bfd22cd19b6948264a20ea9207fb..4a9f4c92953d7ccc7876769e3f99edb241c4a39e 100644
--- a/src/validators/schema/TraverseSchema.hpp
+++ b/src/validators/schema/TraverseSchema.hpp
@@ -777,8 +777,9 @@ private:
     int                                           fCurrentScope;
     int                                           fFinalDefault;
     int                                           fBlockDefault;
-    int                                           fScopeCount;
+    int                                           fScopeCount;    
     unsigned int                                  fAnonXSTypeCount;
+    unsigned int                                  fCircularCheckIndex;
     const XMLCh*                                  fTargetNSURIString;
     DatatypeValidatorFactory*                     fDatatypeRegistry;
     GrammarResolver*                              fGrammarResolver;