I have been trying to import nodes from a Xindice DOM (produced from an
XMLResource retrieved from the Xindice Database) into a DOM created with
the Xerces parser.

This works well until I include attributes in the xml
(http://www.w3.org/XML/1998/namespace) namespace (eg xml:lang) but do
not explicitly declare the mapping from that namespace to the xml prefix
using an xmlns:xml="http://www.w3.org/XML/1998/namespace";.

When I do  include such attributes, I get the error message:

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or
change an object in a way which is incorrect with regard to namespaces.
        at org.apache.xerces.dom.CoreDocumentImpl.checkDOMNSErr(Unknown Source)
        at org.apache.xerces.dom.AttrNSImpl.setName(Unknown Source)
        ...

I have attached a patch to the version of the Xindice code base that I
checked out of SVN this morning that includes a new unit test that shows
this behaviour.

Regards

Geoff Shuetrim
Index: java/tests/src/org/apache/xindice/xml/dom/NodeImportTest.java
===================================================================
--- java/tests/src/org/apache/xindice/xml/dom/NodeImportTest.java	
+++ java/tests/src/org/apache/xindice/xml/dom/NodeImportTest.java	
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Id: ImportTest.java 2008-02-28 12:41:00 +0000 (Thu, 28 Feb 2008) gshuetrim $
+ */
+
+package org.apache.xindice.xml.dom;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import junit.framework.TestCase;
+
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ * Tests NodeImpl class
+ * @version $Revision: 518852 $, $Date: 2007-03-16 03:35:49 +0000 (Fri, 16 Mar 2007) $
+ */
+public class NodeImportTest extends TestCase {
+    
+    private Document okXindiceDoc;
+    private Document okXercesDoc;
+    
+    private Document problemXindiceDoc;
+    private Document problemXercesDoc;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        okXindiceDoc = getXindiceDocument(getOKXml());
+        okXercesDoc = getXercesDocument(getOKXml());
+        problemXindiceDoc = getXindiceDocument(getProblemXml());
+        problemXercesDoc = getXercesDocument(getProblemXml());
+    }    
+    
+    private String getProblemXml() {
+        return "<?xml version='1.0' encoding='UTF-8'?>" +
+        		"<root xml:lang='en'/>";
+    }    
+
+    private String getOKXml() {
+        return "<?xml version='1.0' encoding='UTF-8'?>" +
+        		"<root xmlns:xml='http://www.w3.org/XML/1998/namespace' xml:lang='en'/>";
+    }    
+    
+    public void testExplicitlyDeclaredXMLNamespace() {
+        try {
+            okXercesDoc.importNode(okXindiceDoc.getDocumentElement(),true);            
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Unexpected exception.");
+        }
+    }
+
+    public void testImplicitlyDeclaredXMLNamespace() {
+        try {
+            problemXercesDoc.importNode(problemXindiceDoc.getDocumentElement(),true);            
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Unexpected exception.");
+        }
+    }
+    
+    // DOM Builders
+    
+    private Document getXercesDocument(String xml) throws Exception {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setValidating(false);
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        return builder.parse(new InputSource(new StringReader(xml)));           
+    }    
+    
+    
+    private Document getXindiceDocument(String xml) throws Exception {
+        return DOMParser.toDocument(xml);
+    }
+    
+    private String file = "/home/geoff/test.xml";
+    private String readFile(String file) {
+        String thisLine;
+        StringBuffer strb = new StringBuffer("") ;
+
+        try {
+            FileInputStream fin =  new FileInputStream(file);
+            BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
+            while ((thisLine = myInput.readLine()) != null) {  
+                strb.append(thisLine+"\n");
+            }
+            String result = strb.toString();
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+        return strb.toString();
+    }
+    
+}
+

Reply via email to