[jdom-interest] Namespace issues, et al.

Malachi de AElfweald malachi at tremerechantry.com
Sun Feb 23 20:49:41 PST 2003


Yes, I understand that now.... but I still don't agree with it.
The reason is that doing it that way means that, if I want to say
"use the same Namespace", I have to pass a namespace to the constructor.
If I want to use a different one than the parent (ie: "" instead of 
whatever
it actually is), then I DON'T pass it into the constructor.  The logic is
backwards.

Ie:
CURRENT:
	new Element(name) -> change namespace to ""
	new Element(name, NS) -> change namespace to NS

SHOULD BE:
	new Element(name) -> don't change namespace at all
	new Element(name, NS) -> change namespace to NS

Don't get me wrong. I think JDOM is the best XML API I have ever used.
However,  it is non-intuitive to say "Don't pass a Namespace in the 
constructor
means CHANGE namespace"... It should be "Don't pass a Namespace in the 
constructor
means DON'T CHANGE namespace"...

Malachi

On Sun, 23 Feb 2003 22:36:11 -0600, Bradley S. Huffman 
<hip at a.cs.okstate.edu> wrote:

> Malachi de AElfweald writes:
>
>> The issue was 2-fold. If I created them with the xmlns in the 
>> constructor, I was
>> also setting the xsi:schemaLocation attribute on it again -- thus the 
>> first problem.
>> The second part of it was that I assumed (since textually it is true) 
>> that putting
>> a tag inside another (like tagA above) inherits the namespace of the 
>> parent element.
>> This is true in XML, and is true in the textual representation -- but 
>> JDOM changes the
>> textual representation to break it... Thus, it can not inherit 
>> namespaces from the parent
>> because of the fact xmlns="" is being added without my consent. If that 
>> one piece was not
>> added, then it would all inherit correctly.
>
> JDOM does not change anything.  If you run you text through SAXBuilder,
> JDOM will build a faithful representation of it with JDOM objects 
> (Element,
> Document, DocType, EntityRef, Namespace, and such). It will not change,
> add, or delete any information. The problem seems to be you are creating
> new objects and adding them to the treewhcih was built from your texual
> representation and it's not producing what you think it should.
>
> Let take a simple example, running the following text though SAXBuilder
>
> <root xmlns="my.default.namespace">
> <child1/>
> <child2/>
> </root>
>
> Produces the same objects as the following JDOM code
>
> Namespace defaultNS = Namespace.get("my.default.namespace");
> Element root = new Element("root", defaultNS);
> Text text1 = new Text("\n  ");
> Element child1 = new Element("child1", defaultNS);
> Text text2 = new Text("\n  ");
> Element child2 = new Element("child2", defaultNS);
> Text text3 = new Text("\n");
>
> root.addContent(text1);
> root.addContent(child1);
> root.addContent(text2);
> root.addContent(child2);
> root.addContent(text3);
>
> Document document = new Document(root);
>
> Now if you do a
>
> Element child3 = new Element("child3");
>
> Which is just shorthand for
>
> Element child3 = new Element("child3", Namespace.NO_NAMESPACE);
>
> It produces a new Element object such that
>
> child3.getName()            returns "child3"
> child3.getNamespacePrefix() returns ""
> child3.getNamespaceURI()    returns ""
>
> Then adding it to root Element object above
>
> root.addContent(child3);
>
> Means
>
> child3.getName()            still returns "child3"
> child3.getNamespacePrefix() still returns ""
> child3.getNamespaceURI()    still returns "", not "my.default.namespace"
>
> And running document through XMLOutputter (without setting newlines
> or indent) produces
>
> <root xmlns="my.default.namespace">
> <child1/>
> <child2/>
> <child3 xmlns=""/></root>
>
> If you really want the textual representation to be
>
> <root xmlns="my.default.namespace">
> <child1/>
> <child2/>
> <child3/></root>
>
> i.e child3 is in the "my.default.namespace" namespace, then it should of
> been constructed as follows
>
> Element child3 = new Element("child3", defaultNS);
>
> Again JDOM does not change anything unless explicitly told to do so.
>
> Brad
>
>



-- 
 



More information about the jdom-interest mailing list