[jdom-interest] Namespace Collision exception when adding an additional namespace

PJ Fanning pj.fanning at polarlake.com
Mon May 16 04:37:01 PDT 2005


Hi,
I got an exception when I executed code along the lines of the following:

	Namespace ns1 = Namespace.getNamespace("aaa", "http://acme.com/aaa");
	Element e = new Element("aaa", ns1);
	e.setAttribute("att1", "att1");
	Namespace defns = Namespace.getNamespace("http://acme.com/default");
	e.addNamespaceDeclaration(defns);

I want to declare an element that is in the 'aaa' namespace but that also declares a default namespace that is inherited by other elements. However, I get this exception:

org.jdom.IllegalAddException: The namespace xmlns="http://acme.com/default" could not be added as a namespace to "aaa:aaa": The namespace prefix "" collides with an attribute namespace prefix on the element

I think that the code in the Verifier is incorrect. It seems to assume that attributes that have no explicit namespace are in the default namespace. This is not my understanding of the XML spec.
The code above works if I add the attribute after the additional namespace declaration.

I think that this change resolves the problem:

Verifier.java:
    public static String checkNamespaceCollision(Namespace namespace,
                                                 Attribute attribute) {
        String reason = null;
        if(!attribute.getNamespace().equals(Namespace.NO_NAMESPACE)) {
            reason = checkNamespaceCollision(namespace,
                                                attribute.getNamespace());
            if (reason != null) {
                reason += " with an attribute namespace prefix on the element";
            }
        }
        return reason;
    }



More information about the jdom-interest mailing list