had to change 2 internal parsing API when processing document content to

* include/libxml/parserInternals.h parser.c: had to change
  2 internal parsing API when processing document content
  to check the start and end of element content are defined
  in the same entity
* valid.c include/libxml/valid.h: attribute normalization can
  generate a validity error added xmlValidCtxtNormalizeAttributeValue()
  with the context to report it.
* SAX.c: fixed the last known bugs, crazy validation constraints
  when a document is standalone seems correctly handled. There
  is a couple of open issues left which need consideration especially
  PE93 on external unparsed entities and standalone status.
  Ran 1819 tests: 1817 suceeded, 2 failed and 0 generated an error in 8.26 s.
  The 2 tests left failing are actually in error. Cleanup done.
Daniel
diff --git a/SAX.c b/SAX.c
index cbf21cf..2665998 100644
--- a/SAX.c
+++ b/SAX.c
@@ -830,8 +830,13 @@
      * Needed for HTML too:
      *   http://www.w3.org/TR/html4/types.html#h-6.2
      */
-    nval = xmlValidNormalizeAttributeValue(ctxt->myDoc, ctxt->node,
+    ctxt->vctxt.valid = 1;
+    nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
+	                                   ctxt->myDoc, ctxt->node,
 					   fullname, value);
+    if (ctxt->vctxt.valid != 1) {
+	ctxt->valid = 0;
+    }
     if (nval != NULL)
 	value = nval;
 
@@ -985,16 +990,21 @@
  * Check defaulted attributes from the DTD
  */
 static void
-xmlCheckDefaultedAttributesFromDtd(xmlParserCtxtPtr ctxt,
-	xmlDtdPtr dtd, const xmlChar *name,
+xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
 	const xmlChar *prefix, const xmlChar **atts) {
     xmlElementPtr elemDecl;
     const xmlChar *att;
+    int internal = 1;
     int i;
 
-    if ((dtd == NULL) || (name == NULL))
-	return;
-    elemDecl = xmlGetDtdQElementDesc(dtd, name, prefix);
+    elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
+    if (elemDecl == NULL) {
+	elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
+	internal = 0;
+    }
+
+process_external_subset:
+
     if (elemDecl != NULL) {
 	xmlAttributePtr attr = elemDecl->attributes;
 	/*
@@ -1008,7 +1018,10 @@
 		if ((attr->defaultValue != NULL) &&
 		    (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
 					attr->elem, attr->name,
-					attr->prefix) == attr)) {
+					attr->prefix) == attr) &&
+		    (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
+					attr->elem, attr->name,
+					attr->prefix) == NULL)) {
 		    xmlChar *fulln;
 
 		    if (attr->prefix != NULL) {
@@ -1039,9 +1052,7 @@
 			    ctxt->vctxt.error(ctxt->vctxt.userData,
       "standalone: attribute %s on %s defaulted from external subset\n",
 					      fulln, attr->elem);
-			/* Waiting on the XML Core WG decision on this
 			ctxt->valid = 0;
-			 */
 		    }
 		}
 		attr = attr->nexth;
@@ -1053,7 +1064,18 @@
 	 */
 	attr = elemDecl->attributes;
 	while (attr != NULL) {
-	    if (attr->defaultValue != NULL) {
+	    /*
+	     * Make sure that attributes redefinition occuring in the
+	     * internal subset are not overriden by definitions in the
+	     * external subset.
+	     */
+	    if ((attr->defaultValue != NULL) &&
+		(xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
+				    attr->elem, attr->name,
+				    attr->prefix) == attr) &&
+		(xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
+				    attr->elem, attr->name,
+				    attr->prefix) == NULL)) {
 		/*
 		 * the element should be instantiated in the tree if:
 		 *  - this is a namespace prefix
@@ -1090,13 +1112,20 @@
 			    att = atts[i];
 			}
 		    }
-		    if (att == NULL)
+		    if (att == NULL) {
 			attribute(ctxt, fulln, attr->defaultValue);
+		    }
 		    xmlFree(fulln);
 		}
 	    }
 	    attr = attr->nexth;
 	}
+	if (internal == 1) {
+	    elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
+		                             name, prefix);
+	    internal = 0;
+	    goto process_external_subset;
+	}
     }
 }
 
@@ -1206,12 +1235,7 @@
     if ((!ctxt->html) &&
 	((ctxt->myDoc->intSubset != NULL) ||
 	 (ctxt->myDoc->extSubset != NULL))) {
-	if (ctxt->myDoc->intSubset != NULL)
-	    xmlCheckDefaultedAttributesFromDtd(ctxt, ctxt->myDoc->intSubset,
-		                               name, prefix, atts);
-	if (ctxt->myDoc->extSubset != NULL)
-	    xmlCheckDefaultedAttributesFromDtd(ctxt, ctxt->myDoc->extSubset,
-		                               name, prefix, atts);
+	xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
     }
 
     /*