问题
When I use @XmlSchema(attributeFormDefault = XmlNsForm.QUALIFIED, ...)
or@XmlAttribute(namespace = "sample.com/y", ...)
JAXB ignores @XmlSchema(namespace = "sample.com/x", ...)
and instead of:
<a xmlns="sample.com/y" xmlns:ns0="sample.com/y">
<b ns0:att=""/>
</a>
generates something like:
<ns1:a xmlns:ns1="sample.com/x" xmlns:ns0="sample.com/y">
<ns1:b ns0:att=""/>
</ns1:a>
Is this an expected behavior? Is there any way to correct this?
回答1:
EclipseLink JAXB (MOXy) is handling the prefix qualification for elements differently depending upon the attribute form qualification (as demonstrated below).
The namespace qualification is not wrong, but I agree that the use of default namespace is better when possible. You can track the progress on this issue using the following bug:
- https://bugs.eclipse.org/353042
A
package forum6808921;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class A {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
Demo
package forum6808921;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(A.class);
A a = new A();
a.setB("Hello World");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(a, System.out);
}
}
package-info without attributeFormDefault set
@XmlSchema(
namespace = "sample.com/x"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package forum6808921;
import javax.xml.bind.annotation.*;
Output:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="sample.com/x">
<b>Hello World</b>
</a>
package-info with attributeFormDefault set
@XmlSchema(
namespace = "sample.com/x"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
, attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package forum6808921;
import javax.xml.bind.annotation.*;
Output:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:a xmlns:ns0="sample.com/x">
<ns0:b>Hello World</ns0:b>
</ns0:a>
来源:https://stackoverflow.com/questions/6808921/jaxb-qualified-attributes-disables-default-namespace-xmlns