How to manage the namespace prefix as per XSD used for generating XML ? I have 3 different XSD files, using maven jaxb2-maven-plugin
, I am able to generate Java classes as well. but I am not able to generate XML file with specific namespace prefix as per their corresponding schema. Following is my jaxb2marshaller configuration:
@Bean
public Marshaller ltiMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
Map<String,Object> map = new HashMap<String,Object>();
map.put("jaxb.formatted.output", true);
map.put("com.sun.xml.bind.namespacePrefixMapper", new ManifestNamespaceMapper());
jaxb2Marshaller.setMarshallerProperties(map);
jaxb2Marshaller.setContextPaths(
"com.abc.xyz.generated.basicltilink",
"com.abc.xyz.generated.lommanifest",
"com.abc.xyz.generated.lomresource",
"com.abc.xyz.generated.manifest",
"com.instructure.canvas.xsd.cccv1p0"
);
return jaxb2Marshaller;
}
Following is my ManifestNamespaceMapper.java
public class ManifestNamespaceMapper extends NamespacePrefixMapper {
private Map<String, String> prefixMap = new HashMap<String, String>();
public ManifestNamespaceMapper() {
prefixMap.put("http://www.imsglobal.org/xsd/imsccv1p3/imsccauth_v1p3", "auth");
prefixMap.put("http://ltsc.ieee.org/xsd/imsccv1p3/LOM/manifest", "lomimscc");
prefixMap.put("http://www.imsglobal.org/xsd/imsccv1p3/imscp_v1p1", "");
prefixMap.put("http://www.imsglobal.org/xsd/imsccv1p3/imscsmd_v1p0", "csm");
prefixMap.put("http://ltsc.ieee.org/xsd/imsccv1p3/LOM/resource", "resource");
prefixMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
prefixMap.put("http://canvas.instructure.com/xsd/cccv1p0", "assignment");
}
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion,
boolean requirePrefix) {
return prefixMap.get(namespaceUri);
}
}
Here is the generated XML snippet:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns9:manifest xmlns="http://ltsc.ieee.org/xsd/imsccv1p3/LOM/imscclti"
xmlns:ns2="http://www.imsglobal.org/xsd/imsccv1p2/imscsmd_v1p0"
xmlns:ns3="http://www.imsglobal.org/xsd/imsbasiclti_v1p0" xmlns:ns4="http://www.imsglobal.org/xsd/imslticm_v1p0"
xmlns:ns5="http://www.imsglobal.org/xsd/imslticp_v1p0" xmlns:ns6="http://www.imsglobal.org/xsd/imslticc_v1p2"
xmlns:ns7="http://ltsc.ieee.org/xsd/imsccv1p3/LOM/manifest" xmlns:ns8="http://ltsc.ieee.org/xsd/imsccv1p3/LOM/resource"
xmlns:ns9="http://www.imsglobal.org/xsd/imsccv1p3/imscp_v1p1"
xmlns:ns10="http://www.imsglobal.org/xsd/imsccv1p3/imscsmd_v1p0"
xmlns:ns11="http://www.imsglobal.org/xsd/imsccv1p3/imsccauth_v1p3"
xmlns:ns12="http://canvas.instructure.com/xsd/cccv1p0" identifier="abcd1234">
<ns9:metadata>
<ns9:schema>IMS Common Cartridge</ns9:schema>
<ns9:schemaversion>1.3.0</ns9:schemaversion>
<ns7:lom>....
So if you look on above generated XML, you will find there are many namespaces which don't belong here, they belong to different XML due to different XSD file. Same is the case with other generated XML file, where I see all the namespaces getting inserted.
I am not sure why JAXB2Marshaller is copying everything into every file. Is there any way to segregate the namespacing and namespace prefix for each XML file ?
Update
One way to prevent this issue is to create multiple instance of Jaxb2Marshaller
with different configuration as mentioned here. in the similar thread, there is one answer which suggest to have multiple contextPath which I already did, but didn't helped.
Thanks !