ovi2ut
ovi2ut

Reputation: 197

XML namespace prefix cannot be empty

I'm trying to marshal objects into XML and I have a problem with the namespaces prefix (I don't want to have any prefix).

I'm using bindigs file to customize the generated classes. The generated package-info.java file contains the following:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.test.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = {
@javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.test.com", prefix = "")})
package com.test;

So, the prefix is set to "", but when the XML is generated, I have a generated prefix. If I use another value for prefix (different than an empty string), the XML is generated with the right prefix.

Upvotes: 2

Views: 2379

Answers (2)

Jiankuan Xing
Jiankuan Xing

Reputation: 387

With the latest JAXB, you must set QUALIFIED to elementFormDefault field of XmlSchema annotation. Like this:

@XmlSchema (
    elementFormDefault= XmlNsForm.QUALIFIED,
    ...)

Otherwise, JAXB will add an auto prefix (ns1, ns2, ...)

Upvotes: 2

bdoughan
bdoughan

Reputation: 149007

Your JAXB (JSR-222) implementation does not guarantee that it will use the prefixes specified in the @XmlSchema annotation. EclipseLink JAXB (MOXy) will and so will recent versions of the JAXB RI.

The JAXB RI offers a NamespacePrefixMapper extension which is now supported by MOXy to give more control over the prefixes used.

For More Information

Upvotes: 0

Related Questions