我有两个类似于某种布尔表达式的标签:
<complexType name="condition">
<choice minOccurs="0">
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
</choice>
</complexType>
<complexType name="condition-a">
<all>
<element name="value" type="string"/>
</all>
</complexType>
<complexType name="condition-b">
<all>
<element name="value" type="string"/>
</all>
</complexType>
我想将这些条件与and
,or
和not
结构相结合。像这样:
<condition>
<and>
<condition-a><value>X</value></condition-a>
<condition-b><value>Y</value></condition-b>
<not>
<condition-a><value>Z</value></condition-a>
</not>
</and>
</condition>
请注意,condition
标记仅用于顶部,而不是and
,or
和not
。
我提出了以下定义:
<complexType name="condition">
<choice minOccurs="0">
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
<element name="and" type="myns:condition-and"/>
<element name="or" type="myns:condition-or"/>
<element name="not" type="myns:condition-not"/>
</choice>
</complexType>
<complexType name="condition-a">
<all>
<element name="value" type="string"/>
</all>
</complexType>
<complexType name="condition-b">
<all>
<element name="value" type="string"/>
</all>
</complexType>
<complexType name="condition-and">
<choice maxOccurs="unbounded">
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
<element name="and" type="myns:condition-and"/>
<element name="or" type="myns:condition-or"/>
<element name="not" type="myns:condition-not"/>
</choice>
</complexType>
<complexType name="condition-or">
<choice maxOccurs="unbounded">
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
<element name="and" type="myns:condition-and"/>
<element name="or" type="myns:condition-or"/>
<element name="not" type="myns:condition-not"/>
</choice>
</complexType>
<complexType name="condition-not">
<choice>
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
<element name="and" type="myns:condition-and"/>
<element name="or" type="myns:condition-or"/>
<element name="not" type="myns:condition-not"/>
</choice>
</complexType>
虽然这有效,但并不是最佳选择。我正在重复and
,or
和not
标记中的可能条件。作为副作用,生成的JAXB代码很乱。
对此有更优雅的方法吗?
答案 0 :(得分:1)
XML Schema允许您创建全局 xs:group ( xs:choice ,,而不是多次重复相同的选择> xs:sequence 或 xs:all )并在模式的其他部分引用它,就像全局类型和元素一样。
所以你可以使用这样的东西:
<group name="anyCondition">
<choice>
<element name="a" type="myns:condition-a"/>
<element name="b" type="myns:condition-b"/>
<element name="and" type="myns:condition-and"/>
<element name="or" type="myns:condition-or"/>
<element name="not" type="myns:condition-not"/>
</choice>
</group>
<complexType name="condition-and">
<group ref="myns:anyCondition" maxOccurs="unbounded"/>
</complexType>
<complexType name="condition-or">
<group ref="myns:anyCondition" maxOccurs="unbounded"/>
</complexType>
<complexType name="condition-not">
<group ref="myns:anyCondition"/>
</complexType>