A small patch and more doc, Daniel.
diff --git a/tree.c b/tree.c
index 4de4ee5..b358a5d 100644
--- a/tree.c
+++ b/tree.c
@@ -747,7 +747,7 @@
     }
 
     cur->type = XML_ENTITY_REF_NODE;
-    cur->doc = NULL;
+    cur->doc = doc;
     cur->parent = NULL; 
     cur->next = NULL; 
     cur->prev = NULL;