Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting XML attribute to enum values

Here is the XSD schema that i created for a WS

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="shipmentStatus" type="shipmentStatusType" />

<xs:complexType name="shipmentStatusType">
    <xs:sequence>
        <xs:element name="orderNumber" type="xs:int"/>
    </xs:sequence>

    <xs:attribute name="requestStatus">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="SHIPPED"/>
                <xs:enumeration value="PENDING"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>

</xs:complexType>

When i generated Java classes using JAXB 2.1, it generated only one class i.e. shipmentStatusType. I was expecting that it will generate requestStatus as a JAVA Enum but it didn't. Is it an expected behaviour or did i miss something?

like image 601
Em Ae Avatar asked Dec 06 '25 10:12

Em Ae


1 Answers

Just extract your enum/simple type declaration to top-level one and use it as type of the XML attribute:

<?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>

It will give you such an enum:

/**
 * <p>Java class for requestStatus.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * <p>
 * <pre>
 * &lt;simpleType name="requestStatus">
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *     &lt;enumeration value="SHIPPED"/>
 *     &lt;enumeration value="PENDING"/>
 *   &lt;/restriction>
 * &lt;/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);
    }

}

and class having it:

/**
 * <p>Java class for shipmentStatus complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="shipmentStatus">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="orderNumber" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *       &lt;/sequence>
 *       &lt;attribute name="requestStatus" type="{http://www.example.com}requestStatus" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }

}
like image 114
Michał Majcherski Avatar answered Dec 07 '25 23:12

Michał Majcherski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!