Added c-props-correct constraint to check for equal cardinality of

* xmlschemas.c: Added c-props-correct constraint to check
  for equal cardinality of keyref/key.
* include/libxml/xmlerror.h: Added an error code.
diff --git a/ChangeLog b/ChangeLog
index c5aa488..2771d32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Jul  8 23:35:00 CEST 2005 Kasimier Buchcik <libxml2-cvs@cazic.net>
+
+	* xmlschemas.c: Added c-props-correct constraint to check
+	  for equal cardinality of keyref/key.
+	* include/libxml/xmlerror.h: Added an error code.
+
 Fri Jul  8 21:56:04 CEST 2005 Kasimier Buchcik <libxml2-cvs@cazic.net>
 
 	* pattern.c: Fixed evaluation of attributes. Actually only
diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h
index 03b8ce9..04da1a6 100644
--- a/include/libxml/xmlerror.h
+++ b/include/libxml/xmlerror.h
@@ -759,6 +759,7 @@
     XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3, /* 3077 */
     XML_SCHEMAP_AU_PROPS_CORRECT_2, /* 3078 */
     XML_SCHEMAP_A_PROPS_CORRECT_2, /* 3079 */
+    XML_SCHEMAP_C_PROPS_CORRECT, /* 3080 */
     XML_MODULE_OPEN = 4900, /* 4900 */
     XML_MODULE_CLOSE, /* 4901 */
     XML_CHECK_FOUND_ELEMENT = 5000,
diff --git a/xmlschemas.c b/xmlschemas.c
index fda957a..05ed65e 100644
--- a/xmlschemas.c
+++ b/xmlschemas.c
@@ -7239,6 +7239,8 @@
     item->name = name;
     item->type = idcCategory;
     item->node = node;
+    if (ctxt->assemble != NULL)
+	xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) item);
     /*
     * The target namespace of the parent element declaration.
     */
@@ -17572,27 +17574,50 @@
  */
 static void
 xmlSchemaResolveIDCKeyRef(xmlSchemaIDCPtr idc,
-			  xmlSchemaParserCtxtPtr ctxt,
+			  xmlSchemaParserCtxtPtr pctxt,
 			  const xmlChar * name ATTRIBUTE_UNUSED)
 {
     if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
         return;
     if (idc->ref->name != NULL) {
 	idc->ref->item = (xmlSchemaBasicItemPtr) xmlHashLookup2(
-	    ctxt->schema->idcDef,
+	    pctxt->schema->idcDef,
 	    idc->ref->name,
 	    idc->ref->targetNamespace);
         if (idc->ref->item == NULL) {
 	    /*
 	    * TODO: It is actually not an error to fail to resolve.
 	    */
-	    xmlSchemaPResCompAttrErr(ctxt,
+	    xmlSchemaPResCompAttrErr(pctxt,
 		XML_SCHEMAP_SRC_RESOLVE,
 		(xmlSchemaTypePtr) idc, idc->node,
 		"refer", idc->ref->name,
 		idc->ref->targetNamespace,
 		XML_SCHEMA_TYPE_IDC_KEYREF, NULL);
             return;
+	} else {
+	    if (idc->nbFields !=
+		((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
+		xmlChar *str = NULL;
+		xmlSchemaIDCPtr refer;
+		
+		refer = (xmlSchemaIDCPtr) idc->ref->item;
+		/*
+		* SPEC c-props-correct(2)
+		* "If the {identity-constraint category} is keyref,
+		* the cardinality of the {fields} must equal that of
+		* the {fields} of the {referenced key}.
+		*/
+		xmlSchemaPCustomErr(pctxt,
+		    XML_SCHEMAP_C_PROPS_CORRECT,
+		    NULL, (xmlSchemaTypePtr) idc, idc->node,
+		    "The cardinality of the keyref differs from the "
+		    "cardinality of the referenced key '%s'",
+		    xmlSchemaFormatQName(&str, refer->targetNamespace,
+			refer->name) 
+		);
+		FREE_AND_NULL(str)
+	    }
 	}
     }
 }
@@ -17980,16 +18005,18 @@
 	    case XML_SCHEMA_TYPE_ATTRIBUTE:
 		xmlSchemaAttrFixup((xmlSchemaAttributePtr) item, ctxt, NULL);
 		break;
-	    case XML_SCHEMA_TYPE_ELEMENT:
-		xmlSchemaElementFixup((xmlSchemaElementPtr) item, ctxt,
-		    NULL, NULL, NULL);
-		break;
 	    case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
 		xmlSchemaAttrGrpFixup((xmlSchemaAttributeGroupPtr) item,
 		    ctxt, NULL);
 		break;
 	    case XML_SCHEMA_TYPE_PARTICLE:
 		xmlSchemaMiscRefFixup((xmlSchemaTreeItemPtr) item, ctxt, NULL);
+		break;
+	    case XML_SCHEMA_TYPE_IDC_KEY:
+	    case XML_SCHEMA_TYPE_IDC_UNIQUE:
+	    case XML_SCHEMA_TYPE_IDC_KEYREF:
+		xmlSchemaResolveIDCKeyRef((xmlSchemaIDCPtr) item, ctxt, NULL);
+		break;
 	    default:
 		break;
 	}
@@ -18035,6 +18062,22 @@
 		ctxt, NULL);
 	}
     }
+    if (ctxt->nberrors != 0)
+	return;
+    for (i = 0; i < nbItems; i++) {
+	item = items[i];
+	switch (item->type) {
+	    case XML_SCHEMA_TYPE_ELEMENT:
+		xmlSchemaElementFixup((xmlSchemaElementPtr) item, ctxt,
+		    NULL, NULL, NULL);
+		break;
+	    default:
+		break;
+	}
+    }
+    if (ctxt->nberrors != 0)
+	return;
+
     /*
     * Fixup for simple/complex types.
     */
@@ -24292,7 +24335,7 @@
 
     if ((ctxt == NULL) || (input == NULL))
         return (-1);
-
+    
     memset(&schemas_sax, 0, sizeof(xmlSAXHandler));
     schemas_sax.initialized = XML_SAX2_MAGIC;
     if (sax == NULL) {