[jdom-interest] The default namespace behaviour

Edelson, Justin Justin.Edelson at mtvn.com
Tue Jun 6 21:02:01 PDT 2006


> JDOM seems to have no concept of a default namespace other than perhaps the
> document builder.  This seems to very much be erroneous.
Not really. JDOM does have a concept of a default namespace. It's a namespace without a prefix. You are correct that default namespace inheritance is not directly supported except that in output XMLOutputter is smart enough to not define a namespace over and over again (for input, the SAX parser handles this detail and JDOM actually has no way of knowing where a namespace gets set or how). I think this is appropriate given that JDOM is an object model and the namespace is a property of each object.
 
> Constructing the first XML example in memory with:
> Element easy = new Element("easy", "short");
> Element b = new Element("b");
> Element c = new Element("c");
> ...
> easy.addContent(b);
> b.addContent(c);
> constructs b and c with no namespace which is not as it should be. 
But that's what you asked it to do when you called new Element("b") and new Element("c"). If you want them to have the namespace URI "short", then just use the appropriate contructor. There might be an argument to be made that, under certain circumstances, Elements without a namespace should inherit the namspace of their parent, I personally think the resulting code would be significantly more unreadable. It's obvious what's going on when the lines are one after the other (as in your example), but JDOM doesn't require that. If those three constructors were in different methods, it becomes less clear what namespace you mean which elements to be in. The difference is really a single line of code to create the Namespace and a few extra characters as the namespace is explicitly passed to each appropriate constructor. I would write the (full) code to generate the first example as:
 
        Namespace nsShort = Namespace.getNamespace("short");
        Namespace nsWeird = Namespace.getNamespace("e", "weird");
        
        Document doc = new Document();
        Element easy = new Element("easy", nsShort);
        doc.setRootElement(easy);
        Element b = new Element("b", nsShort);
        Element c = new Element("c", nsShort);
        easy.addContent(b);
        b.addContent(c);
        
        Element d = new Element("d", nsShort);
        Element f = new Element("f", nsWeird);
        f.addContent(new Element("g", nsShort));
        d.addContent(f);
        easy.addContent(d);
 
In general, I don't really understand what it is you're trying to accomplish. From your examples, it would appear that you want to move the element with the localName "easy" and namespace URI "short" to the namespace URI "ReallyLongNamespace" and move the remaining elements in the "short" namespace to no namespace. If so, you could do something like:
 
        for (Iterator it = doc.getDescendants(); it.hasNext();) {
            Object o = it.next();
            if (o instanceof Element) {
                Element el = (Element) o;
                if ("short".equals(el.getNamespaceURI())) {
                    if ("easy".equals(el.getName())) {
                        el.setNamespace(Namespace.getNamespace("a", "ReallyLongNamespace"));
                    } else {
                        el.setNamespace(Namespace.NO_NAMESPACE);
                    }
                }
            }
        }
 
That doesn't seem like too much code.
 
I strongly disagree with your sense that:
a.setNamespace(namespace.getNamespace("ReallyLongNamespace"));
 
should set the namespace for all child elements that use the same default namespace as a. This would be a big leap for JDOM to make. Since it's entirely legitimate to have different default namespaces at different levels, having JDOM guess which ones you wanted to change would invariably be incorrect (in other words, what if I really wanted setNamespace() to just set that element's namespace). Writing the getDescendents()-using code above or even better, as Mr. Huffman suggested, a custom JDOMFactory would be the best way to go about explictingly specifying what transformation you wanted to do. 
 
________________________________

From: jdom-interest-bounces at jdom.org on behalf of Colin LeMahieu
Sent: Tue 6/6/2006 9:59 PM
To: 'Bradley S. Huffman'
Cc: jdom-interest at jdom.org
Subject: RE: [jdom-interest] The default namespace behaviour 



I'm thinking more along the lines of, the user types an XML file:
<easy xmlsn="short">
        <b>
                <c/>
        </b>
        <d>
                <e:f xmlns:e="weird">
                        <g/>
                </e:f>
        </d>
</easy>

And I'm looking to convert it to:
<a:easy xmlns:a="ReallyLongNamespace">
        <b>
                <c/>
        </b>
        <d>
                <e:f xmlns:e="weird">
                        <g/>
                </e:f>
        </d>
</a>

As I see it right now, with the current JDOM model there's no way to easily
just change the namespace of a and affect elements b, c, d, and g as it
should.

JDOM seems to have no concept of a default namespace other than perhaps the
document builder.  This seems to very much be erroneous.

Constructing the first XML example in memory with:
Element easy = new Element("easy", "short");
Element b = new Element("b");
Element c = new Element("c");
...
easy.addContent(b);
b.addContent(c);

constructs b and c with no namespace which is not as it should be.  My quote
earlier is from someone claiming that this is the correct behaviour, "if you
don't know what namespace an element is in, you don't know what element
you're creating"  I say it's incorrect, or otherwise tell me a way to do:

a.setNamespace(namespace.getNamespace("ReallyLongNamespace"));

and have it affect b, c, d, and a possible g somewhere down the XML tree
that would inherit the default namespace from a.

----------------
Colin LeMahieu
Phone: 408-499-5269
Fax: 267-989-4575
Email: clemahieu at gmail.com


-----Original Message-----
From: Bradley S. Huffman [mailto:hip at cs.okstate.edu]
Sent: Tuesday, June 06, 2006 13:30
To: Colin LeMahieu
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] The default namespace behaviour

"Colin LeMahieu" writes:

> Essentially my problem is that I have a lot of longhand names and
namespaces
> and a user will only pick a subset of them to use in a particular
document.
> I would like to allow shorthand names in different namespaces and then
> translate them to the longhand forms.  The document would then be
processed
> further.

How about a custom JDOMFactory that overrides the element methods.

Brad

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060607/b427e548/attachment.htm


More information about the jdom-interest mailing list