[jdom-interest] Namespace inheritance after cloning
Alistair Young
alistair at smo.uhi.ac.uk
Mon Nov 29 02:03:22 PST 2004
It would be nice, if, when you addContent to an Element, if the new
Content didn't have any namespaces, JDOM didn't add the empty
namespace. Rather, it would just assume the new Content was to inherit
the original namespace. Of course, if a namespace is associated with
the new Content, then JDOM would honour this and add it.
Just a thought...
Alistair
On 29 Nov 2004, at 07:40, Jason Hunter wrote:
> Off topic but related to the namespace idea, one thing I've found is
> there's no efficient way to have "modes" like you're talking about
> that apply at construction time.
>
> Statics are global to a class loader and thus aren't real appropriate
> in a J2EE environment where different people may use JDOM with the
> same class loader. Thread local variables are slow. This is why J2EE
> uses factories *so* much, and makes modes an aspect of the factory.
>
> We faced the issue when trying to have a mode to turn off
> verification. In the end we implemented a factory-based solution.
>
> -jh-
>
> Per Norrman wrote:
>> Hi,
>> I've been harbouring (spelling?) an idea where there were two distinct
>> modes of Element/Attribute creation. One namespace unaware, in which
>> it would be impossible to create Elements in any namespace but
>> NO_NAMESPACE; and one namespace aware mode, where it would be
>> impossible to create Elements without
>> explicitly declaring their namespace. Lot's of headaches would be
>> alleviated.
>> Not sure how to solve it though, except a Design Rule: When dealing
>> with
>> namespaces, ALWAYS use the namespace enabled constructors.
>> /pmn
>> Jason Hunter wrote:
>>> Alistair Young wrote:
>>>
>>>> I have a strange problem. I've read the archives and the FAQ and
>>>> understand the non inheritance of prefix:local style namespaces.
>>>> However,
>>>> when I do:
>>>>
>>>> Document root =
>>>> <root xmlns="http://test.ns">
>>>> </root>
>>>>
>>>> Document next =
>>>> <next>some text</next>
>>>>
>>>> root.getRootElement().addContent((Content)next.getRootElement().clon
>>>> e());
>>>>
>>>> I get:
>>>> <root xmlns="http://test.ns">
>>>> <next xmlns="">some text</next>
>>>> </root>
>>>>
>>>> and it breaks the XML schema validation! <next> should obviously be
>>>> in the
>>>> test.ns namespace. JDOM seems to want to put it in an empty
>>>> namespace.
>>>
>>>
>>>
>>> The <next> you created is in no namespace. When you move it to
>>> another document, the element doesn't change namespaces. Namespaces
>>> are an intrinsic part of each Element and don't depend on placement.
>>> So on output JDOM has to make sure that the default namespace on
>>> <root> doesn't mistakenly apply to <next> so it writes xmlns=""
>>> which reassigns the default namespace for that context to no
>>> namespace. JDOM's correctly serializing the XML data model as you
>>> created it.
>>>
>>>> Now, the weird thing is, if I create <next> with the same namespace
>>>> as
>>>> <root>, JDOM still replaces it with xmlns="".
>>>
>>>
>>>
>>> That's not something JDOM would do. I suspect your code isn't
>>> actually doing what you describe here. :) If you can't figure it
>>> out, send in a simple code sample showing the issue and we'll see if
>>> it's any real bug.
>>>
>>>> If <root> doesn't have a namespace, then the xmlns="" isn't added
>>>> to <next>.
>>>> Nowhere in the code is xmlns="" generated when creating the <next>
>>>> Element.
>>>
>>>
>>>
>>> Right, because there's no need to redefine the default namespace
>>> when there's no default namespace in effect.
>>>
>>> -jh-
>>>
>>> _______________________________________________
>>> To control your jdom-interest membership:
>>> http://www.jdom.org/mailman/options/jdom-interest/
>>> youraddr at yourhost.com
>>>
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/
>> youraddr at yourhost.com
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
More information about the jdom-interest
mailing list