2012-08-13 113 views
3

这里是我要WS限制XML属性的枚举值

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

当我生成使用JAXB 2.1 Java类中创建的XSD架构,它仅产生一个类即shipmentStatusType。我期待它会生成requestStatus作为JAVA Enum,但它没有。这是预期的行为还是我错过了什么?

回答

4

只需提取你的枚举/简单类型声明顶级之一,并把它作为类型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> 
* &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); 
    } 

} 

和类:

/** 
* <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; 
    } 

} 
+0

您不必使全局类型使其工作。有些情况下XSD不能或不应该被改变。对于提供的XSD,解决方案实际上是使用自定义绑定文件。 – 2012-08-14 01:50:23

1

我认为你在问同样的事情as this SO post。您需要创建一个自定义绑定文件来将此简单类型映射到枚举。

绑定文件:

<jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" version="2.1"> 
    <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true"> 
     <xjc:serializable/> 
    </jaxb:globalBindings> 
    <jaxb:bindings schemaLocation="file:/..../restricting-xml-attribute-to-enum-values.xsd"> 
     <jaxb:bindings node="//xs:complexType[@name='shipmentStatusType']/xs:attribute[@name='requestStatus']/xs:simpleType"> 
      <jaxb:typesafeEnumClass name="MyEnumType"/> 
     </jaxb:bindings> 
    </jaxb:bindings> 
</jaxb:bindings> 

生成的类(相关部分):

/** 
* <p>Java class for null. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* <p> 
* <pre> 
* &lt;simpleType> 
* &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 = "") 
@XmlEnum 
public enum MyEnumType { 

    SHIPPED, 
    PENDING; 

    public String value() { 
     return name(); 
    } 

    public static ShipmentStatusType.MyEnumType fromValue(String v) { 
     return valueOf(v); 
    } 

} 
+0

不,我已经workign的方式 “因此” 建议这样做。但它没有工作 – 2012-08-13 21:25:00

+0

@EmAe,我不同意。这意味着你没有遵循这个想法。事情是你不必修改XSD,你应该避免要求你这样做的解决方案,除非该工具不能真正处理它。 – 2012-08-14 01:44:45