Search code examples
jaxbxsdjaxb2xsd-validation

JAXB: Ignore order of elements while using xs:extension


We are using JAXB for Java-xml binding.We initially created domain class and then using schemagen commandline tool the following schema has been generated. But generated schema is not valid, giving following error message.

Error Message: cos-all-limited.1.2: An all model group must appear in a particle with {min occurs} = {max occurs} = 1, and that particle must be part of a pair which constitutes the {content type} of a complex type definition.

Use Case: There are two classes Emp(Base class) and Dept(Child class).
1. There is no restriction on the elements sequence(means empId, deptId and deptName can appear in any order). so we used xs:all element
2. In Dept class, deptId field should appear only once(minOccurs =1, maxOccurs=1) and deptName is optional.
As per my usecase i am unable to generate valid schema. I did search on google. But i couldn't find the solution. So i am anticipating experts can answer this query. Could you please look into below classes,schema and guide me in the right direction.

NOTE: please don't suggest me to create some temporary domain classes.
Thanks in anticipation.

Emp.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="EmpType", propOrder={})
@XmlRootElement
public class Emp {

    @XmlElement(name="empId", required = true)
    private String empId;
}

Dept.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="DeptType", propOrder={})
public class Dept extends Emp
{

    @XmlElement(name="deptId", required = true)
    private String deptId;
    private String deptName;
}

Schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="emp" type="EmpType"/>

  <xs:complexType name="EmpType">
    <xs:sequence>
      <xs:element name="empId" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="DeptType">
    <xs:complexContent>
      <xs:extension base="EmpType">
        <xs:all>  <!--showing error message, mentioned above -->
          <xs:element name="deptId" type="xs:string" minOccurs="1" maxOccurs="1"/>
          <xs:element name="deptName" type="xs:string" minOccurs="0"/>
        </xs:all>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

Solution

  • The document structure that you are trying to allow is actually difficult to represent in an XML schema. You may not be able to generate it from a JAXB (JSR-222) annotated model. You do however have a couple of options:

    Option #1 - Generate a Simpler XML Schema

    If you are not validating XML content with your XML schema and are simply using it as documentation that people can use as a guide then I would drop the all sections and use sequence instead. This will work better with the inheritance relationship that you have. If you don't specify an instance of Schema on the Unmarshaller the order of elements is not enforced so you will be able to read all the XML documents that meet your rules.

    Option #2 - Create Your Own XML Schema

    If you want the XML schema to exactly reflect all the possible accepted inputs then you will need to write it yourself. You can reference this existing XML schema by using the package level @XmlSchema annotation.

    @XmlSchema(location = "http://www.example.com/package/YourSchema.xsd")
    package com.example;
    
    import javax.xml.bind.annotation.XmlSchema;