[jdom-interest] Bug and query

Rolf Lear rlear at algorithmics.com
Wed Jun 25 05:44:43 PDT 2003

Hate to be the one again ... ;-)

No, it's not useless. Elements with the same tagname in different namespaces
are very different Elements.

Your example:

     <tag xmlns="mynamespace">
         <childtag xmlns="">
            some data

The root element in your example is in the Namespace  "mynamespace"

The childElement is in the namespace "".

These are very different namespaces!

Embellising the example to:

     <tag xmlns="mynamespace">
         <childtag xmlns="">
            some data
            some data

The second childtag is in the "mynamespace" Namespace, and the first is in
the "" (or NO_NAMESPACE) namespace.

NO_NAMESPACE is a very useful construct that indicates that an element has
NO NAMESPACE (surprise surprise), and NOT having a namespace is just as
important as HAVING a namespace. If you want your elements to have a
namespace, then you MUST tell JDOM what namespace to give it.

The following code creates a JDom document that represents the embellished

Namespace myns = Namespace.getNamespace("mynamespace");
// Create the root element that looks like <tag xmlns="mynamespace">
Element tag = new Element("tag", myns);
//******  Create an element with NO NAMESPACE    *********
//**** in our example it will look like <childtag xmlns="">
Element firstchild = new Element ("childtag");
firstchild.addContent("some data");
//******  Create an element with the SAME Namspace as the root...
//**** in our example it will look like <childtag> because once we
//     then <childtag xmlns="mynamspace"> is redundant and we only need to
have <childtag>
Element secondchild = new Element ("childtag", myns);
secondchild.addContent("some data");

Have a look through, and figure out where your confusion is. Although in XML
it feels like a Namespace is "inherited", when you create an Element in
JDom, you have to tell JDom what Namespace it is in.

The Parser you are using is not an XML compliant parser. Use one that is.

Before using namespaces in a document, learn how they work, and how they are
specified in JDom.

Changing the way JDom currently does Namespaces would not be adding a
Feature, but breaking it.


-----Original Message-----
From: Eric VERGNAUD [mailto:eric.vergnaud at wanadoo.fr]
Sent: Wednesday, June 25, 2003 8:27 AM
To: 'jdom-interest at jdom.org'
Subject: Re: [jdom-interest] Bug and query 

le 25/06/03 13:01, Rolf Lear à rlear at algorithmics.com a écrit :

> Hate to be the one to inform you, but JDom is correct, and the produced
> XML exactly represents the XML you asked JDom to create.

I'm glad to be informed, I'm not a member of the "I'm the one who's right"
congregation, to which many programmers seem to belong.

> Creating an Element without a Namespace argument with "new Element
> (tagname)" is exactly equivalent to creating an element as "new Element
> (tagname, Namespace.NO_NAMESPACE)". If you want to put an element into a
> namespace, you have to tell JDom what namespace to use.

Yes this was fine.

> Further, xmlns="" is perfectly valid, and a parser which does not parse it
> is not XML Compliant.

BUT it's useless (isn't it ?). So let's call this a feature request rather
than a bug, even though it ends up with a bug in my situation because the
client parser (a very specialized one, and apparently a pretty bogus one)
does not seem to support empty namespaces.

> Have a look at http://www.w3.org/TR/REC-xml-names/#defaulting and scan
down to
> where it says "The default namespace can be set to the empty string. This
> the same effect, within the scope of the declaration, of there being no
> default namespace."

Thanks very much for this useful link.


To control your jdom-interest membership:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030625/4f7745a6/attachment.htm

More information about the jdom-interest mailing list