By default, the contents of a sequence element can only appear once in an instance of a complex type. You can change the number of times the sequence of elements defined by a sequence element is allowed to appear using its minOccurs attribute and its maxOccurs attribute. Using these attributes you can specify that the sequence type can occur zero to an unlimited number of times in an instance of a complex type.
The minOccurs attribute specifies the minimum number of times the sequence must occur in an instance of the defined complex type. Its value can be any positive integer. Setting the minOccurs attribute to 0 specifies that the sequence does not need to appear inside an instance of the complex type.
The maxOccurs attribute specifies the upper limit for how many times the sequence can occur in an instance of the defined complex type. Its value can be any non-zero, positive integer or unbounded. Setting the maxOccurs attribute to unbounded specifies that the sequence can appear an infinite number of times.
Example 13.20 shows the definition of a sequence type, CultureInfo, with sequence occurrence constraints. The sequence can be repeated 0 to 2 times.
Example 13.20. Sequence with Occurrence Constraints
<complexType name="CultureInfo">
<sequence minOccurs="0" maxOccurs="2">
<element name="Name" type="string"/>
<element name="Lcid" type="int"/>
</sequence>
</complexType>Unlike single instance sequences, XML Schema sequences that can occur multiple times are mapped to a Java class with a
single member variable. This single member variable is a List<T> object that holds all of the data
for the multiple occurrences of the sequence. For example, if the sequence defined in
Example 13.20 occurred two times, then the list would have four
items.
The name of the Java class' member variable is derived by concatenating the names of the member elements. The element
names are separated by And and the first letter of the variable name is converted to lower case. For example,
the member variable generated from Example 13.20 is named
nameAndLcid.
The type of object stored in the list depends on the relationship between the types of the member elements. For example:
If the member elements are of the same type the generated list will contain
JAXBElement<T> objects. The base type of the
JAXBElement<T> objects is determined by the normal mapping of the member elements'
type.
If the member elements are of different types and their Java representations implement a common interface, the list will contains objects of the common interface.
If the member elements are of different types and their Java representations extend a common base class, the list will contain objects of the common base class.
If none of the other conditions are met, the list will contain Object objects.
The generated Java class only has a getter method for the member variable. The getter method returns a reference to the live list. Any modifications made to the returned list effects the actual object.
The Java class is decorated with the @XmlType annotation. The annotation's
name property is set to the value of the name attribute from the parent
element of the XML Schema definition. The annotation's propOrder property contains the single member
variable representing the elements in the sequence.
The member variable representing the elements in the sequence are decorated with the
@XmlElements annotation. The @XmlElements annotation
contains a comma separated list of @XmlElement annotations. The list has one
@XmlElement annotation for each member element defined in the XML Schema definition of
the type. The @XmlElement annotations in the list have their name
property set to the value of the XML Schema element element's
name attribute and their type property set to the Java class resulting from
the mapping of the XML Schema element element's type.
Example 13.21 shows the Java mapping for the XML Schema sequence defined in Example 13.20.
Example 13.21. Java Representation of Sequence with an Occurrence Constraint
@XmlType(name = "CultureInfo", propOrder = {
"nameAndLcid"
})
public class CultureInfo {
@XmlElements({
@XmlElement(name = "Name", type = String.class),
@XmlElement(name = "Lcid", type = Integer.class)
})
protected List<Serializable> nameAndLcid;
public List<Serializable> getNameAndLcid() {
if (nameAndLcid == null) {
nameAndLcid = new ArrayList<Serializable>();
}
return this.nameAndLcid;
}
}