只需将您的枚举/简单类型声明提取到顶层,并将其用作 XML 属性的类型:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com" xmlns="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:simpleType name="requestStatus">
<xs:restriction base="xs:string">
<xs:enumeration value="SHIPPED" />
<xs:enumeration value="PENDING" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shipmentStatus">
<xs:sequence>
<xs:element name="orderNumber" type="xs:int" />
</xs:sequence>
<xs:attribute name="requestStatus" type="requestStatus" />
</xs:complexType>
<xs:element name="shipmentStatus" type="shipmentStatus" />
</xs:schema>
它会给你这样一个枚举:
/**
* <p>Java class for requestStatus.
*
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* <pre>
* <simpleType name="requestStatus">
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
* <enumeration value="SHIPPED"/>
* <enumeration value="PENDING"/>
* </restriction>
* </simpleType>
* </pre>
*
*/
@XmlType(name = "requestStatus")
@XmlEnum
public enum RequestStatus {
SHIPPED,
PENDING;
public String value() {
return name();
}
public static RequestStatus fromValue(String v) {
return valueOf(v);
}
}
和拥有它的班级:
/**
* <p>Java class for shipmentStatus complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="shipmentStatus">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="orderNumber" type="{http://www.w3.org/2001/XMLSchema}int"/>
* </sequence>
* <attribute name="requestStatus" type="{http://www.example.com}requestStatus" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "shipmentStatus", propOrder = {
"orderNumber"
})
public class ShipmentStatus {
protected int orderNumber;
@XmlAttribute(name = "requestStatus")
protected RequestStatus requestStatus;
/**
* Gets the value of the orderNumber property.
*
*/
public int getOrderNumber() {
return orderNumber;
}
/**
* Sets the value of the orderNumber property.
*
*/
public void setOrderNumber(int value) {
this.orderNumber = value;
}
/**
* Gets the value of the requestStatus property.
*
* @return
* possible object is
* {@link RequestStatus }
*
*/
public RequestStatus getRequestStatus() {
return requestStatus;
}
/**
* Sets the value of the requestStatus property.
*
* @param value
* allowed object is
* {@link RequestStatus }
*
*/
public void setRequestStatus(RequestStatus value) {
this.requestStatus = value;
}
}