[英]jaxb: xsd to java, a parent element that can have one of two other elements
I have a parent element called MESSAGE
. 我有一个称为MESSAGE
的父元素。 The MESSAGE
element can carry any kind of OBJECT
(usually complexTypes), but only one object at a time. MESSAGE
元素可以携带任何类型的OBJECT
(通常为complexTypes),但一次只能携带一个对象。 I'm starting from XML files to XSD then to java with something like this: 我从XML文件开始到XSD,然后再到Java,如下所示:
<xs:element name="MESSAGE">
<xs:complexType>
<xs:sequence>
<xs:element name="OBJECT"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
I have lots of XML files which have MESSAGE
types but contain different OBJECT
s. 我有许多具有MESSAGE
类型但包含不同OBJECT
的XML文件。 I have produced their equivalent XSDs but only the first one is translated to java using xjc, while the rest are not because of the error Message is already defined
. 我已经制作了它们等效的XSD,但是只有第一个使用xjc转换为java了,而其余的并不是因为错误Message is already defined
。 So how can I have generic MESSAGE
element in my XSD which can take any OBJECT
s? 那么,如何在我的XSD中具有可以接收任何OBJECT
的通用MESSAGE
元素?
I asked a similar question to this one once, and I think it would give you an idea how to do this: 我曾经问过与此问题类似的问题,并且我认为这将使您知道如何执行此操作:
How to Override Xsd element inside of parent/extended element 如何在父元素/扩展元素内覆盖Xsd元素
You'll have to extend/restrict your root parent object. 您必须扩展/限制根父对象。
If the element inside MESSAGE
must be one of a subset of defined elements, use <choice>
. 如果MESSAGE
的元素必须是已定义元素的子集之一,请使用<choice>
。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MESSAGE">
<xsd:complexType>
<xsd:choice>
<xsd:element name="A" type="xsd:string"/>
<xsd:element name="B" type="xsd:string"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
If the element inside MESSAGE
can be any defined element, use <any>
. 如果MESSAGE
的元素可以是任何已定义的元素,请使用<any>
。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MESSAGE">
<xsd:complexType>
<xsd:sequence>
<any namespace="##targetNamespace"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="X" type="xsd:string"/>
<xsd:element name="Y" type="xsd:string"/>
</xsd:schema>
I solved it using the minoccurs
and maxoccurs
like this. 我使用minoccurs
和maxoccurs
这样解决了它。 but it doesn't adhere precisely to what i was asking . 但是它并不完全符合我的要求 。 the accepted answer is better 可接受的答案更好
<xs:element name="MESSAGE">
<xs:complexType>
<xs:sequence>
<xs:element name="A" minOccurs="0" maxOccurs="1"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="B" minOccurs="0" maxOccurs="1"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
It was a bit cumbersome adding all the elements to one xsd file , but it worked, i have tested it. 将所有元素添加到一个xsd文件中比较麻烦,但是它确实有效,我已经对其进行了测试。 @Andreas answer seems more readable using <xsd:choice>
. @Andreas答案使用<xsd:choice>
更具可读性。 i have never used it, but i will look into it. 我从未使用过它,但我会对其进行研究。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.