fixing #96925 wich was also dependant on the processing of parsed

* parser.c xpath.c: fixing #96925 wich was also dependant on the
  processing of parsed entities, and XPath computation on sustitued
  entities.
* testXPath.c: make sure entities are substitued.
Daniel
diff --git a/ChangeLog b/ChangeLog
index a6b89b6..c4f090d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sat Nov 16 16:30:25 CET 2002 Daniel Veillard <daniel@veillard.com>
+
+	* parser.c xpath.c: fixing #96925 wich was also dependant on the
+	  processing of parsed entities, and XPath computation on sustitued
+	  entities.
+	* testXPath.c: make sure entities are substitued.
+
 Fri Nov 15 16:22:54 CET 2002 Daniel Veillard <daniel@veillard.com>
 
 	* parser.c: fixed #96594, which was totally dependant on the 
diff --git a/parser.c b/parser.c
index 6fa4d0b..ca56c1d 100644
--- a/parser.c
+++ b/parser.c
@@ -5456,6 +5456,7 @@
 				} else {
 				    while (list != NULL) {
 					list->parent = (xmlNodePtr) ctxt->node;
+					list->doc = ctxt->myDoc;
 					if (list->next == NULL)
 					    ent->last = list;
 					list = list->next;
@@ -9767,9 +9768,9 @@
 xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
 	const xmlChar *string, void *user_data, xmlNodePtr *lst) {
     xmlParserCtxtPtr ctxt;
-    xmlDocPtr newDoc;
+    xmlDocPtr newDoc = NULL;
     xmlSAXHandlerPtr oldsax = NULL;
-    xmlNodePtr content;
+    xmlNodePtr content = NULL;
     int size;
     int ret = 0;
 
@@ -9794,32 +9795,28 @@
 
     oldsax = ctxt->sax;
     ctxt->sax = oldctxt->sax;
-    newDoc = xmlNewDoc(BAD_CAST "1.0");
-    if (newDoc == NULL) {
-	ctxt->sax = oldsax;
-	xmlFreeParserCtxt(ctxt);
-	return(-1);
-    }
-    if (oldctxt->myDoc != NULL) {
-	newDoc->intSubset = oldctxt->myDoc->intSubset;
-	newDoc->extSubset = oldctxt->myDoc->extSubset;
-    }
-    newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
-    if (newDoc->children == NULL) {
-	ctxt->sax = oldsax;
-	xmlFreeParserCtxt(ctxt);
-	newDoc->intSubset = NULL;
-	newDoc->extSubset = NULL;
-        xmlFreeDoc(newDoc);
-	return(-1);
-    }
-    nodePush(ctxt, newDoc->children);
     if (oldctxt->myDoc == NULL) {
-	ctxt->myDoc = oldctxt->myDoc;
-    } else {
+	newDoc = xmlNewDoc(BAD_CAST "1.0");
+	if (newDoc == NULL) {
+	    ctxt->sax = oldsax;
+	    xmlFreeParserCtxt(ctxt);
+	    return(-1);
+	}
 	ctxt->myDoc = newDoc;
-	newDoc->children->doc = newDoc;
+    } else {
+	ctxt->myDoc = oldctxt->myDoc;
+        content = ctxt->myDoc->children;
     }
+    ctxt->myDoc->children = xmlNewDocNode(newDoc, NULL,
+	                                  BAD_CAST "pseudoroot", NULL);
+    if (ctxt->myDoc->children == NULL) {
+	ctxt->sax = oldsax;
+	xmlFreeParserCtxt(ctxt);
+	if (newDoc != NULL)
+	    xmlFreeDoc(newDoc);
+	return(-1);
+    }
+    nodePush(ctxt, ctxt->myDoc->children);
     ctxt->instate = XML_PARSER_CONTENT;
     ctxt->depth = oldctxt->depth + 1;
 
@@ -9829,14 +9826,7 @@
     ctxt->validate = 0;
     ctxt->loadsubset = oldctxt->loadsubset;
 
-    if (ctxt->myDoc != NULL) {
-        content = ctxt->myDoc->children;
-        ctxt->myDoc->children = NULL;
-        xmlParseContent(ctxt);
-        ctxt->myDoc->children = content;
-    } else {
-        xmlParseContent(ctxt);
-    }
+    xmlParseContent(ctxt);
     if ((RAW == '<') && (NXT(1) == '/')) {
 	ctxt->errNo = XML_ERR_NOT_WELL_BALANCED;
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
@@ -9852,7 +9842,7 @@
 	ctxt->wellFormed = 0;
 	ctxt->disableSAX = 1;
     }
-    if (ctxt->node != newDoc->children) {
+    if (ctxt->node != ctxt->myDoc->children) {
 	ctxt->errNo = XML_ERR_NOT_WELL_BALANCED;
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
 	    ctxt->sax->error(ctxt->userData,
@@ -9877,20 +9867,23 @@
 	 * Return the newly created nodeset after unlinking it from
 	 * they pseudo parent.
 	 */
-	cur = newDoc->children->children;
+	cur = ctxt->myDoc->children->children;
 	*lst = cur;
 	while (cur != NULL) {
 	    cur->parent = NULL;
 	    cur = cur->next;
 	}
-	newDoc->children->children = NULL;
+	ctxt->myDoc->children->children = NULL;
+    }
+    if (ctxt->myDoc != NULL) {
+	xmlFreeNode(ctxt->myDoc->children);
+        ctxt->myDoc->children = content;
     }
 	
     ctxt->sax = oldsax;
     xmlFreeParserCtxt(ctxt);
-    newDoc->intSubset = NULL;
-    newDoc->extSubset = NULL;
-    xmlFreeDoc(newDoc);
+    if (newDoc != NULL)
+	xmlFreeDoc(newDoc);
     
     return(ret);
 }
diff --git a/testXPath.c b/testXPath.c
index 98f482d..8d13808 100644
--- a/testXPath.c
+++ b/testXPath.c
@@ -171,6 +171,7 @@
     if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
     xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
     xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
+    xmlSubstituteEntitiesDefaultValue = 1;
     if (nocdata != 0) {
 	xmlDefaultSAXHandlerInit();
 	xmlDefaultSAXHandler.cdataBlock = NULL;
diff --git a/xpath.c b/xpath.c
index 5da280f..28c8a36 100644
--- a/xpath.c
+++ b/xpath.c
@@ -1367,6 +1367,10 @@
     if (node1 == node2->next)
 	return(-1);
 
+#if 0
+    Unfortunately this does not work. Line number in entities reset
+    to 1 within the entity :-(
+
     /*
      * Speedup using line numbers if availble.
      */
@@ -1381,7 +1385,7 @@
 	if (l1 > l2)
 	    return(-1);
     }
-
+#endif
     /*
      * compute depth to root
      */
@@ -5169,13 +5173,27 @@
     }
 
     if (cur->children != NULL) {
-    	if (cur->children->type != XML_ENTITY_DECL)
-	    return(cur->children);
+	/*
+	 * Do not descend on entities declarations
+	 */
+    	if (cur->children->type != XML_ENTITY_DECL) {
+	    cur = cur->children;
+	    /*
+	     * Skip DTDs
+	     */
+	    if (cur->type != XML_DTD_NODE)
+		return(cur);
+	}
     }
 
     if (cur == ctxt->context->node) return(NULL);
 
-    if (cur->next != NULL) return(cur->next);
+    while (cur->next != NULL) {
+	cur = cur->next;
+	if ((cur->type != XML_ENTITY_DECL) &&
+	    (cur->type != XML_DTD_NODE))
+	    return(cur);
+    }
     
     do {
         cur = cur->parent;