users@jaxb.java.net

Re: Suppress namespace declarations

From: Claus Nagel <claus.nagel_at_gmail.com>
Date: Mon, 20 Oct 2008 08:18:50 +0200

Thanks for your answer and sorry for my late response. I would like to
add two more examples to make clear why I would prefer to directly
tell the marshaller to suppress namespace declarations. Perhaps there
are also quite simple solutions for these examples...

A.
Given the schemas A, B, and C from my previous example, I want to have
an application that reads an instance document, changes some of its
contents, and writes it back to disk again. Since I cannot be sure
whether this document contains elements from A+B+C, or just A+B or A+C
I would initialize a JAXBContext covering all schemas. By doing so, I
can be sure to cover all possibilities. However, having read the file,
the application could realize that the document only contains elements
from A+B. So for writing the document back to disk I would like to
suppress the namespace of C. Given your suggested solution, I would
have to create a new JAXBContext, but since that is time and memory
consuming I would in contrast prefer a simple way to tell the
marshaller to suppress the namespace of C for the already existing
JAXBContext.

B.
Think of the following situation: Schema A <xs:import>s schemas B and
C. Schema A defines its own types but also extends type definitions
from B and C. Thus, a valid XML instance document for schema A may
just contain elements from A, but also any combination from A, B, and
C. The generated Java classes for A, B, and C still go into different
packages. Again, I want an application to marshall a content tree for
schema A. The difference to my previous example is that I just have to
set up the JAXBContext for schema A. Schemas B and C are implicitly
known to JAXB because of the <xs:import>s of B and C.

If I set up the JAXBContext for schema A and if I have a content tree
for some document just containing elements from A and B, how should I
tell the marshaller to suppress the namespace for C? Obviously, your
solution will not work in this example, right?

Any suggestions welcome!
Claus





2008/9/24 Wolfgang Laun <wolfgang.laun_at_gmail.com>:
> Since B and C are in different namespaces, the generated Java classes
> will be in different packages. After building the content tree for some
> document, you ought to know whether you have one with elements
> from A+B+C, or only A+B or A+C. You may now set up
> JAXBContext based on the actually used packages. Then there will
> be no namespace declaration for the spurious package.