Antwort: RE: [jdom-interest] Namespace help

bob mcwhirter bob at werken.com
Thu Jul 25 07:43:05 PDT 2002


On Thu, 25 Jul 2002, Li Xu wrote:

> That's why this whole thing is so confusing to me. Elliotte and Bob both
> said "" is not a namespace, you said "" is a namespace ... Furthermore,
> if level3 can inherit level2's namespace without a xmlns=, then why didn't
> level2 do so and inherit level1's namespace thus get rid of xmlns=""?
> 
> I guess my original question is still unclear. I'm not having any
> problem with namespace or the parser. All java objects I have, either
> a Document or an Element, are exactly correct. The issue is with the
> Outputter, when it's printing out a XML string why is it outputting
> 'xmlns=""' when it knows that <level2> is inheriting the namespace form
> <root_elem> which is in "http://my.com" namespace?

<level2> is not in the namespace "http://my.com", so for it to
inherit the no-prefix namespace of "http://my.com" would be
semantically wrong.

My questions for you:

1) What is the namespace URI for <level1>?  I claim it's
   "http://my.com".

2) What is the namespace URI for <level2>?  You claim you
   constructed it without a namespace, so I claim that the
   namespace URI is "".

3) What is the namespace URI for <level3>?  You claim,
   like with <level2>, that you constructed it without
   a namespace, so I claim that the namespace URI is "".

Given this document:

    <level1 xmlns="http://my.com">
      <level2>
        <level3/>
      </level2>
    </level1>

The <level1> element is indeed in the "http://my.com" namespace.  But,
alas, the <level2> element is also in the "http:/my.com" namespace, 
which according to #2 above, would be wrong.  Likewise, <level3> is
also in the "http://my.com" namespace, which according to #3 above
is also quite wrong.

So, we -have- to adjust our namespace so that <level2> ends up having
the namespace URI of "".  That is done by adding the xmlns="" attribute.

    <level1 xmlns="http://my.com">
      <level2 xmlns="">
        <level3/>
      </level2>
    </level1>

Now, at this point, <level1> is in "http://my.com", which is right.
<level2> is in "" which is also right.  How about <level3>?
Well, since i inherits all of the ancestor namespace declarations,
and one of its ancestors has defined the no-prefix case to be
the "" namespace URI, then <level3> is indeed in the "" namespace.

So, all is well.

Remember, you explicitly set the namespaces when you construct
each element.  No inheritence is really happening within the
Document/Element object model.

The XML characters above are a representation of the object model,
and follow various specifications, like the XML-Namespace spec.
It allows namespace-prefix inheritence and all sorts of other fun
stuff.

Really, and I stress, this, you need to read the specs and try
to understand them.  This really isn't a JDOM-specific question
at all.

	-bob




More information about the jdom-interest mailing list