[jdom-interest] Namespace help

Clint M. Frederickson clintf at neo.cs.montana.edu
Thu Jul 25 08:12:59 PDT 2002


If the namespace of a child element is the *same* as it's parent,
XMLOutputter won't print it. If there is any question as to what
namespace an Element belongs to just print out the element with
System.out.println(). You will notice that if a child has the same
namespace as the parent XMLOutputter won't print it at all. In the
example below patientInfo isn't in the same namespace as it's parent
(patient) so it's namespace is printed out. OTOH, patientID is in the
same namespace so it doesn't get printed out. 

Hopefully this clears up the issue for you. It sounds like you may just
have some misconceptions about namespaces in general (They are pretty
confusing). I recommend you purchase Elliotte's book "Processing XML
with Java" when it comes out in November. It's quite good. I recommend
the following sections for you from the online edition:

For future reference, Elliotte's book is a very good resource for
questions about JDOM and XML in general.

--Clint M. Frederickson

Root namespace: [Namespace: prefix "" is mapped to URI
[Element: <patient [Namespace: http://somenamepace]/>]
[Element: <patientID [Namespace: http://somenamepace]/>]
[Element: <patientInfo/>]

<?xml version="1.0" encoding="UTF-8"?>
<patient xmlns="http://somenamepace"
xsi:schemaLocation="http://somenamepace somenamespace.xsd">
  <patientID />
  <patientInfo xmlns="" />

On Wed, 2002-07-24 at 12:46, Li Xu wrote:
> Hi,
> I'm having a problem on properly setting namespaces. I have a element
tree built without any namespaces assigned to any elements. When I set
the root element's namespace to something, it appears that all the 2nd
level child elements now have an namespace that's an empty string, ie
<level2_elem xml=""> when using XMLOutputter.outputString()
> After debugging in to JDOM's source code, I found that the way the
outputter works is that it checks the parent element's namespace URI and
if the child have a different namespace URI it will print out the
child's namespace, otherwise if they are the same then no xmlns="xyz"
will be printed out. XMLOutputter.java line#1618. (Just to be exact, the
check is done between the child and all its ancesters not just the
immediate parent)
> Now I don't know if this is a bug or FAD (feature as designed), but if
the child namespace is null thus its URI is null, then this can be
interpreted as the child implicitly inherits the parent's namespace
(using elementFormDefault="qualified" attribute in the schema should be
all it needs). But the current JDOM implementation pretty much says
"null" is not the same as a specified parent namespace, thus I will
print out this empty namespace URI even though xmlns="" looks really
> The consequence for me is that my client app receives this XML output
from the server as a string. If schema validation is turned on on the
client side when it parses the doc, then the XML is invalid because of
this xmlns="" issue.
> Can someone suggest what my options are here?
> Thanks!
> _______________________________________________
> To control your jdom-interest membership:

More information about the jdom-interest mailing list