JAXB验证错误-cvc-complex-type.2.4.a:发现无效的内容(从元素'codeSystem'开始)。预期为“ {codeSystem}”之一

raw*_*215 0 java xml validation parsing jaxb

我正在尝试解析和验证XML(jaxb-impl-2.2.4.jar),但出现错误:

cvc-complex-type.2.4.a:发现无效的内容(从元素“ codeSystem”开始)。预期为“ {codeSystem}”之一。

我不确定是什么原因造成的,因为我认为我的XML看起来正确。

codeSystem的架构要求:

<xs:complexType name="GenericPropertyType">
     <xs:element name="codeSystem" type="tns:CodeSystem">
     </xs:element>
     <xs:element name="code" type="tns:Code">
     </xs:element>
     <xs:element name="codeText" type="tns:CodeText" minOccurs="0">
     </xs:element>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

codeSystem所属的GenericProperty Java类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GenericPropertyType", propOrder = {
    "codeSystem",
    "code",
    "codeText"
})
public class GenericPropertyType {

    @XmlElement(required = true)
    protected String codeSystem;
    @XmlElement(required = true)
    protected String code;
    @XmlElement
    protected String codeText;

    /**
     * Getters and Setters ommitted.
     * 
     */
}
Run Code Online (Sandbox Code Playgroud)

解析的XML:

<genericProperty>
    <codeSystem>8B-30-33</codeSystem>
    <code>EMAIL_RETRY_COUNT</code>
    <codeText>5</codeText>
</genericProperty>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过在genericPropertycodeSystem元素中不提供名称空间的情况,xmlns="http://www.somedomain.com/context"但是错误仍然相同。有任何想法吗?

编辑 模式中的CodeSystem类型:

<xs:simpleType name="CodeSystem">
<xs:annotation>
  <xs:documentation>Simple Type with Input Restictions</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
  <xs:whiteSpace value="collapse" fixed="true"/>
  <xs:maxLength value="64" fixed="true"/>
</xs:restriction>
Run Code Online (Sandbox Code Playgroud)

raw*_*215 5

通过将以下行添加到针对(XSD文件)进行验证的XmlSchema中,我能够解决此问题:elementFormDefault =“ qualified”

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.somedomain.com/context"
        xmlns:tns="http://www.somedomain.com/context"
        **elementFormDefault="qualified"**>
Run Code Online (Sandbox Code Playgroud)

我在我的package-info.java类中声明了它,但是我不愿检查它是否在提供给JAXB解析器的XSD模式中。