Xml Schema / JaxB - 如何在架构中启用空枚举,字符串,整数值而不验证错误? [英] Xml Schema / JaxB -- How to enable empty enumerations, string, integer values in schema without validation errors?

查看:44
本文介绍了Xml Schema / JaxB - 如何在架构中启用空枚举,字符串,整数值而不验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几件事:

1)

<xs:simpleType name="matchAnalysisType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="A"/>
        <xs:enumeration value=""/>
    </xs:restriction>
 </xs:simpleType>

JaxB不生成枚举,而是将matchAnalysisType标记为相应元素类型的字符串。

JaxB DOESN'T generate enums and instead marks matchAnalysisType as string for the corresponding element type.

2)使用'nillable':

2) Use 'nillable':

 <xs:element name="matchAnalysisType" type="matchAnalysisType"  
     nillable="true">                
 </xs:element>

JaxB抛出''无效的错误。

JaxB throws error that '' is not valid.

该问题适用于以下其他元素类型:

The issue holds true for other element types like the following:

 <xs:element name="accountNumber" minOccurs="0">                
     <xs:simpleType>
         <xs:restriction base="xs:integer">
             <xs:totalDigits value="9"/>
         </xs:restriction>
     </xs:simpleType>
  </xs:element>

我想要一个示例xml,允许以下内容而不会出现任何验证错误。

I'd like to have a sample xml that allows this the following without any validation errors.

 <accountNumber></accountNumber> 

想法?

推荐答案

JAXB没有与对应的枚举值的默认枚举值名称。如果使用外部绑定文件提供名称,则JAXB实现可以生成与此XML模式类型对应的Java枚举。

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

使用 -b 参数在XJC调用中指定绑定文件:

The binding file is specified in the XJC call using the -b parameter:

xjc -b binding.xml your-schema.xsd

更多信息

  • http://blog.bdoughan.com/2011/08/jaxb-and-enums.html

这篇关于Xml Schema / JaxB - 如何在架构中启用空枚举,字符串,整数值而不验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆