For example, I\'ve got a simple schema which imports another schema. The second schema (urn:just:attributes, just-attributes.xsd) just defines an attribute group.
There is a way of doing this, which uses an internal JAXB implementation class called NamespacePrefixMapper
. In the JAXB RI, this is in com.sun.xml.bind.marshaller
, but in Java6, it's in com.sun.xml.internal.bind.marshaller
.
This is an abstract class, which you can subclass and implement the abstract method which maps namespace URIs on to prefixes.
You then inject an instance of that subclass into the marshaller:
JAXBContext context = ...
Marshaller marshaller = context.createMarshaller();
NamespacePrefixMapper prefixMapper = new MyPrefixMapperImpl();
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", prefixMapper);
The property name is going to be different for the Java6 version, but you get the idea.
Note that this is an internal JAXB implementation class, so there's no guarantee it'll be there in future versions.