Index: DOMFactory.java
===================================================================
RCS file: /home/cvspublic/cocoon-2.1/src/java/org/apache/cocoon/util/jxpath/DOMFactory.java,v
retrieving revision 1.1
diff -u -r1.1 DOMFactory.java
--- DOMFactory.java	9 Sep 2003 14:23:08 -0000	1.1
+++ DOMFactory.java	23 Jan 2004 20:26:21 -0000
@@ -53,6 +53,7 @@
 import org.apache.commons.jxpath.AbstractFactory;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.Pointer;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
@@ -79,20 +80,18 @@
          //FIXME: JXPath automatically creates attributes if the element already exists,
          //but does not call this method if the element does not exit 
 
-        addDOMElement((Element) parent, index, name);
+        addDOMElement((Node) parent, index, name);
         
         return true;
     }
 
-    private void addDOMElement(Element parent, int index, String tag) {
+    private void addDOMElement(Node parent, int index, String tag) {
         int pos = tag.indexOf(':');
         String prefix = null;
         if (pos != -1) {
             prefix = tag.substring(0, pos);
         }
-        String uri = getNamespaceURI(parent, prefix);
-                
-        //System.out.println("Found namespace '" + uri + "' for tag " + tag);
+        String uri = null;
         
         Node child = parent.getFirstChild();
         int count = 0;
@@ -102,10 +101,31 @@
             }
             child = child.getNextSibling();
         }
+        
+        Document doc = parent.getOwnerDocument();
+        
+        if (doc != null) {
+            uri = getNamespaceURI((Element)parent, prefix);
+        }
+        else {
+            if (parent instanceof Document) {
+                doc = (Document)parent;
+                if (prefix != null) {
+                    throw new RuntimeException("Cannot map non-null prefix " +
+                        "when creating a document element");    
+                }
+            }
+            else { // Shouldn't happen (must be a DocumentType object)
+                throw new RuntimeException("Node of class " +
+                    parent.getClass().getName() + " has null owner document " +
+                    "but is not a Document"); 
+            }
+
+        }
 
         // Keep inserting new elements until we have index + 1 of them
         while (count <= index) {
-            Node newElement = parent.getOwnerDocument().createElementNS(uri, tag);
+            Node newElement = doc.createElementNS(uri, tag);
             parent.appendChild(newElement);
             count++;
         }
