Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

PJ Fanning

2005-05-16


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;
  }

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.