How can I avoide XSD sequence when generating XSDs

2019-07-22 00:50发布

问题:

When I have annotaded java class like

@javax.xml.bind.annotation.XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UserdataType {

    String username;
    String street;
    String address;

it will be generated to

<xs:complexType name="userdataType">
<xs:sequence>
<xs:element name="username" type="xs:string" minOccurs="0"/>
<xs:element name="street" type="xs:string" minOccurs="0"/>
<xs:element name="address" type="xs:string" minOccurs="0"/>

So, by default JAX-WS always generates 'sequences' in XSD files.

This forces the clients to take care of the exact order the elements, which is not helpful in some cases.

Is there a way to generate something different then sequences?

回答1:

Add an XmlType annotation with an empty propOrder, like this:

 @XmlType(propOrder={})

It will then generate an xs:all (which is unordered) instead of a sequence.

<xs:complexType name="userdataType">
  <xs:all>
    <xs:element name="username" type="xs:string" minOccurs="0"/>
    <xs:element name="street" type="xs:string" minOccurs="0"/>
    <xs:element name="address" type="xs:string" minOccurs="0"/>
  </xs:all>
</xs:complexType>