2

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 !

lexicore
  • 42,748
  • 17
  • 132
  • 221
Aman Gupta
  • 5,548
  • 10
  • 52
  • 88
  • First, are the XML correct? i.e. elements are correctly prefixed? If so, other namespaces are unused and should not be a big problem. I think all namespaces are added because you added corresponding packages on `jaxb2Marshaller.setContextPaths(...)`. – LMC Jan 31 '18 at 15:41
  • Yes XML structure is correct but namespace are wrong. I mean those prefixed namespaces e.g. ns2 ns4 ns5 etc. `` I do not want tags to be like this. is there any other way than `jaxb2marshaller.setContextpath` ? – Aman Gupta Jan 31 '18 at 15:43
  • Your marshaller is taking into account all possible cases according to contextpaths I think. – LMC Jan 31 '18 at 15:45
  • So there is no way to get rid of those unwanted namespace ? I think its not good to have them getting added in every single xml element – Aman Gupta Jan 31 '18 at 15:50
  • See example 8 [here](https://www.javatips.net/api/com.sun.xml.bind.marshaller.namespaceprefixmapper). It's removing unused ones which I don't like lol, but may work for you. – LMC Jan 31 '18 at 15:56

0 Answers0