XJC不会在xs:union内部生成枚举 [英] XJC does not generate enum inside xs:union

查看:84
本文介绍了XJC不会在xs:union内部生成枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个XSD文件,其中包含如下结构:

I have several XSD files containing structures like the following:

<xs:complexType name="SomeThing" abstract="false">
    <xs:sequence>
        <xs:element name="id" type="schema2:SomeIdTypeClass" minOccurs="1" maxOccurs="1"/>
        <xs:element name="myType" type="schema1:MyType" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

<xs:simpleType name="MyType">
    <xs:union>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                    <xs:enumeration value="APPLE"/>
                    <xs:enumeration value="ORANGE"/>
                    <xs:enumeration value="BANANA"/>
                </xs:restriction>
            </xs:simpleType>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                <xs:pattern value="OTHER:[a-zA-Z_][a-zA-Z0-9_]*"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>

我正在使用XJC命令在控制台上生成Java类,而不是maven或ant或任何其他构建工具.

I am using the XJC command to generate the Java classes on the console, not maven or ant or any other build tool.

除此类枚举外,所有类似乎都生成良好.使用枚举类型的类仅将其引用为String,例如 Something 类包含 MyType 仅作为字符串,而不像我期望的那样作为ENUM.

All Classes seem to be generated fine except these kind of enums. The Classes that are using the enum Type are only referecning it as String, e.g. the Something class contains MyType only as a String, and not as an ENUM, as I would expect it.

在另一个文件中,我有类似这样的内容,其中枚举的生成没有问题,引用类按应有的方式将其用作枚举.

In another file I have something like this, where the Enums are generated without problems and referencing classes are using it as Enums as they should.

<xs:simpleType name="SizeType">
<xs:restriction base="xs:string">
        <xs:enumeration value="SMALL"/>
        <xs:enumeration value="MEDIUM"/>
        <xs:enumeration value="LARGE"/>
</xs:restriction>

我不想替换XSD文件,因为它们不是由我自己生成的,并且我有一些包含许多元素的文件.

I do not want to alternate the XSD files since they are not generated by myself and I have a few files with many of those elements.

我得出的结论是,我需要使用某种XJB绑定文件,但我不知道到底需要在其中写些什么:

I came to the conclusion that i need to use some kind of XJB binding file, but I cannot figure out, what exactly i need to write there:

以下内容仅显示错误导致目标节点过多"

The following gives me only the error "results in too many target nodes"

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
jaxb:extensionBindingPrefixes="xjc">
    <jaxb:bindings schemaLocation="MySchema.xsd" node="/xs:schema">
        <jaxb:bindings node="//xs:simpleType[@name='MyType']/xs:union/xs:simpleType">
            <jaxb:typesafeEnumClass name="MyType"/>
        </jaxb:bindings>    
    </jaxb:bindings>
</jaxb:bindings>

添加 multiple ="true" 可以解决此问题,但仍然无法提供所需的枚举.

Adding multiple="true" solves that but still does not give me the Enums that I want.

当我删除xs:union以便拥有正常的xs:simpleType时:

When i remove the xs:union so that i have a normal xs:simpleType like this:

<xs:simpleType name="MyType">
    <xs:restriction base="xs:string">
    <xs:pattern value="OTHER:[a-zA-Z_][a-zA-Z0-9_]*"/>
    <xs:enumeration value="APPLE"/>
    <xs:enumeration value="ORANGE"/>
    <xs:enumeration value="BANANA"/>
    </xs:restriction>             
</xs:simpleType>

然后按预期工作.我仍然不想走那条路,如果不需要的话,可以编辑模式.一定有办法解决.

Then it works as intended. I still dont want to go down that route and edit the schemas if I dont have to. There must be some way around.

推荐答案

好吧,我已经花了一些时间在这个主题上,我在Google上搜索了很多并进行了一些测试.

Ok I have been spending some time on the subject, I googled a lot and made some tests.

简短答案:这不可能

详细答案:

关于SO的问题是关于一个有同样问题的人,但更简单:他有一个包含单个simpleType(带有一些enumerationrestriction)的union,这很奇怪,因为union是毫无意义的,但无论如何.

This question on SO is about a guy having the same issue but simpler : he had an union containing a single simpleType (a restriction with some enumeration), which is weird because the union is pointless but anyway.

所以这个家伙被卡住了,这个答案很有意思:

So the guy is stuck and this answer was quite interesting :

工会似乎没有得到很好的支持 https://jaxb.java.net/tutorial/section_2_2_10_Defining -a-Type-Union.html

It looks like unions are not well supported https://jaxb.java.net/tutorial/section_2_2_10-Defining-a-Type-Union.html

如果您可以修改架构,则可以提取枚举 将匿名简单类型转换为命名类型,然后在您的代码中使用 为此新类型生成的Java枚举....

If you could modify the schema you could extract the enumerated anonymous simple type to a named type and then use in your code the Java enum generated for this new type....

没有很好的支持...但是不如一点也不"或在某些情况下"一样?

Not well supported... But not well as in "not at all" or as in "in some case" ?

不幸的是,链接已断开,但多亏了我们的朋友 Internet档案

Unfortunalely the link is broken but thanks to our friend The Internet Archive, I pulled out the information I needed :

在Java中,没有便捷的方式来表达简单的并集 类型. JAXB编译器只需在任何位置插入Java的String类型 使用联合类型,并将其留给应用程序程序员 处理其余的.尽量避免使用xsd:union.

In Java, there is no convenient way of expressing unions of simple types. The JAXB compiler simply inserts Java's String type wherever the union type is used and leaves it up to the application programmer to handle the rest. Try to avoid xsd:union.

这就是路的尽头,您被困住了.只要您的架构中存在union,就不会生成emum类,并且始终会选择String作为默认类型.

So that's the end of the road, you are stuck. As long as union are present in your schema, no emum class will be generated and String will always be picked as default type.

此外,我发现了这篇文章,在XML模式中扩展枚举列表.在某些解决方案中,作者主张使用union,但最终会说:

Moreover, I found this article, Extend enumerated lists in XML schema. In some solutions, the author advocates to use union but ultimately say :

需要<xsd:union>标记支持,该标记有时无法在工具中实现

Requires <xsd:union> tag support, which is sometimes not implemented in tools

最后,最好的解决方案可能是文章中的第一个:编辑原始架构以包括新的枚举值

Finally, the best solution is maybe the first one in the article : Edit the original schema to include the new enumerated values

这篇关于XJC不会在xs:union内部生成枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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