0

I'm generating XML using JAXB and need specific namespace prefixes for my XML elements. Here's an example of the XML structure I am currently generating, where each namespace has a unique prefix:

<ns4:RequestPayload 
    xmlns:head="urn:iso:std:iso:20022:tech:xsd:head.001.001.03" 
    xmlns:doc="urn:iso:std:iso:20022:tech:xsd:admi.004.001.02"  
    xmlns:ns4="urn:bceao:pi:xsd:payload.1.0.0" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <head:AppHdr>
        <!-- Header info --> 
        <head:Sgntr>            
            <ds:Signature>
                <!-- Signature info --> 
            </ds:Signature>
        </head:Sgntr>
    </head:AppHdr>
    <doc:Document>
        <!-- Document info --> 
    </doc:Document>
</ns4:RequestPayload>

When I omit the prefix namespace declarations in my package-info.java, a default naming convention is applied, resulting in prefixes like ns1, ns2, ns3:

<ns4:RequestPayload 
    xmlns:ns1="urn:iso:std:iso:20022:tech:xsd:head.001.001.03" 
    xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:admi.004.001.02"  
    xmlns:ns4="urn:bceao:pi:xsd:payload.1.0.0" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ns1:AppHdr>
        <!-- Header info --> 
        <ns1:Sgntr>            
            <ds:Signature>
                <!-- Signature info --> 
            </ds:Signature>
        </ns1:Sgntr>
    </ns1:AppHdr>
    <ns2:Document>
        <!-- Document info --> 
    </ns2:Document>
</ns4:RequestPayload>

My goal is to generate an XML without any namespace prefixes, except for the "ds" prefix for the signature elements.

I tried overriding the getPreferredPrefix method in com.sun.xml.bind.marshaller.NamespacePrefixMapper to achieve the desired namespace prefix customization. Here is my implementation:

public class CustomNamespacePrefixMapper extends NamespacePrefixMapper {
    @Override
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        if ("http://www.w3.org/2000/09/xmldsig#".equals(namespaceUri)) {
            return "ds";
        }
        return "";
    }
}

And adding to :

JAXBContext context = JAXBContext.newInstance(EnvelopeAdmi004.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new CustomNamespacePrefixMapper());

Despite this implementation, the output XML still contains default prefixes like ns1, ns2, and ns3. I expected that my custom NamespacePrefixMapper would result in an XML with no prefixes, except for the ds prefix for the signature elements.

How can I achieve the desired XML format with only the ds prefix and no other prefixes? What am I missing in my approach? Any suggestions or insights would be greatly appreciated.

4
  • The namespace have schemas including the ds namepsace and you cannot remove the namespaces if they are referenced in the ds namespace. Commented Jul 21, 2024 at 20:09
  • I want to remove all prefixes on namespaces except the the one with ds Commented Jul 22, 2024 at 11:37
  • @jdweng I want something like this: <RequestPayload xmlns="urn:bceao:pi:xsd:payload.1.0.0" > <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.03" > <!-- Header info --> <Sgntr> <ds:Signature xmlns:ds="w3.org/2000/09/xmldsig#"> <!-- Signature info --> </ds:Signature> </Sgntr> </AppHdr> <Document xmlns="urn:iso:std:iso:20022:tech:xsd:admi.004.001.02" > <!-- Document info --> </Document> </RequestPayload> Commented Jul 22, 2024 at 11:39
  • Set boolean requirePrefix to false , or set prefix to null, or set prefix to "" which is an empty string. Commented Jul 22, 2024 at 15:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.