[jdom-interest] Namespace issues, et al.
Malachi de AElfweald
malachi at tremerechantry.com
Sun Feb 23 15:30:54 PST 2003
Carefully trying to recreate the code from work.... this is what I came up
with...
#1 was my fault. It was caused because I was adding the schemaLocation to
each element
as well as the xmlns and xmlns:xsi
#2 however does do that (as per the FAQ)...:
Namespace xmlns = Namespace.getNamespace("http://www.temporal-
wave.com/spec");
Namespace xsins =
Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
Attribute xsiloc = new Attribute("schemaLocation", "http://www.temporal-
wave.com/spec jbase.xsd", xsins);
Element outer = new Element("outer", xmlns);
outer.addNamespaceDeclaration(xsins);
outer.setAttribute(xsiloc);
Document doc = new Document(outer);
Element middle = new Element("middle");
outer.addContent(middle);
Element inner = new Element("inner");
middle.addContent(inner);
XMLOutputter out = new XMLOutputter(" ", true);
out.output(doc, System.out);
So, here's the deal.... if I explicitely pass xmlns to the constructor for
middle and inner,
then XMLOutputter is CORRECTLY ripping them out when it outputs them (see
my comment about #1 above).
However, that is not intuitive. The reason is that the above code more
correctly represents the visual
(textual) representation of the XML I want output. I see that this relates
directly to the question about
"moving" elements around. On the other hand, I would argue that the
"inner" element isn't IN the document
until it is added, so the above is not trying to "move" it around.
I haven't looked at cvs to see what the code actually looks like, but the
most natural way for a JDOM
developer who is just starting to use namespaces would be to do:
Element outer = new Element("outer");
outer.setAttribute("xmlns", "http://www.temporal-wave.com/spec");
outer.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-
instance");
outer.setAttribute("xsi:schemaLocation", "http://www.temporal-
wave.com/spec jbase.xsd");
Document doc = new Document(outer);
The reason I say this is more natural is because we see some xml format
(say the w3schools tutorial),
and type in exactly what we see.... though I understand the spec says that
those are reserved, etc...
the current format is non-intuitive...
Needless to say, I know what I need to do to make the code work... I have
to explicitely add the xmlns
(and xmlns:xsi and fo and...???) and NOT include the xsi:schemaLocation on
each attribute...
In regards to the XML1.1 issue.... JDOM obviously doesn't seem to care if I
am reading a document stated
version="1.1".... is there any reason I can't send "1.1" so that they know
I am using Unicode 3? Is there
anything in JDOM that will just break? How do I pass version="1.1"?
Malachi
On Sun, 23 Feb 2003 17:56:54 -0500, Elliotte Rusty Harold
<elharo at metalab.unc.edu> wrote:
> At 2:44 PM -0800 2/23/03, Malachi de AElfweald wrote:
>
>> If you try to create that document, you get one of two scenarios...
>> 1) every element says xmlns="http://www.temporal-wave.com/spec" (thus,
>> lots of overhead over the network)
>> 2) the top element says that, the next one says xmlns="", and then
>> everything inside that doesn't show xmlns at all.
>
> You've either uncovered a bug in the Serializer or there's a bug in your
> code, probably related to misuse of namespaces. I strongly suspect the
> latter, but the former is possible. If you could show a simple test case
> we could tell you which.
--
More information about the jdom-interest
mailing list