Xml Schema / JaxB -- How to enable empty enumerations, string, integer values in schema without validation errors?
I tried a couple of things:
1)
<xs:simpleType name="matchAnalysisType">
<xs:restriction base="xs:string">
<xs:enumeration value="A"/>
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
JaxB DOESN'T generate enums and instead marks matchAnalysisType as string for the corresponding element type.
2) Use 'nillable':
<xs:element name="matchAnalysisType" type="matchAnalysisType"
nillable="true">
</xs:element>
JaxB throws error that '' is not valid.
The issue holds true for other element types like the follo开发者_JAVA技巧wing:
<xs:element name="accountNumber" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="9"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I'd like to have a sample xml that allows this the following without any validation errors.
<accountNumber></accountNumber>
Thoughts?
JAXB does not have a default enum value name for enum values corresponding to "". Your JAXB implementation can generate a Java enum corresponding to this XML schema type if you use an external bindings file to provide a name.
binding.xml
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="your-schema.xsd">
<jxb:bindings node="//xs:simpleType[@name='matchAnalysisType']/xs:restriction/xs:enumeration[@value='']">
<jxb:typesafeEnumMember name="BLANK"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC Call
The binding file is specified in the XJC call using the -b
parameter:
xjc -b binding.xml your-schema.xsd
For More Information
- http://blog.bdoughan.com/2011/08/jaxb-and-enums.html
Here is the solution :
tyesafeEnumMemberName has by default value generateError and you can specify value generateName.
This attribute was not present in our xjc file. Now this fixes the autogneration.
<jxb:bindings schemaLocation="your.xsd" node="/xsd:schema">
<jxb:globalBindings typesafeEnumMaxMembers="9000" typesafeEnumMemberName="generateName" >
<xjc:simple/>
</jxb:globalBindings>
精彩评论