我的要求是将XSD中的元素限制为某些数字
<xs:element name="RequestType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="001"/>
<xs:enumeration value="002"/>
<xs:enumeration value="003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
我也试过使用绑定xml,但这也无法正常工作
<jxb:bindings node="//xs:simpleType[@name='RequestType']/xs:restriction/xs:enumeration[@value='001']">
<jxb:typesafeEnumMember name="Inquiry"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='RequestType']/xs:restriction/xs:enumeration[@value='002']">
<jxb:typesafeEnumMember name="Confirmation"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='RequestType']/xs:restriction/xs:enumeration[@value='003']">
<jxb:typesafeEnumMember name="Force Payment"/>
</jxb:bindings>
有人可以帮忙解决这个问题。
答案 0 :(得分:0)
我终于能够通过使用“使用组合映射对枚举类型进行内联自定义”来解决此问题
以下是示例
<xs:element name="RequestType" type="Request"/>
<xs:simpleType name="Request">
<xs:annotation>
<xs:appinfo>
<jaxb:typesafeEnumClass>
<jaxb:typesafeEnumMember value="1" name="Inquiry" />
<jaxb:typesafeEnumMember value="2" name="Confirmation" />
<jaxb:typesafeEnumMember value="3" name="Force_Payment" />
</jaxb:typesafeEnumClass>
</xs:appinfo>
</xs:annotation>
<xs:restriction base="xs:int">
<xs:enumeration value="1" />
<xs:enumeration value="2" />
<xs:enumeration value="3" />
</xs:restriction>
这将生成枚举请求
@XmlType(name = "Request")
@XmlEnum(Integer.class) public enum Request {
@XmlEnumValue("1")
Inquiry(1),
@XmlEnumValue("2")
Confirmation(2),
@XmlEnumValue("3")
Force_Payment(3);
private final int value;
Request(int v) {
value = v;
}
public int value() {
return value;
}
public static Request fromValue(int v) {
for (Request c: Request.values()) {
if (c.value == v) {
return c;
}
}
throw new IllegalArgumentException(String.valueOf(v));
}