XSD - Re-use/inheritance 个标签

XSD - Re-use/inheritance of tags

我有两个类似于某种布尔表达式的标签:

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

我想将这些条件与 andornot 结构结合起来。像这样:

<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标签只用在顶部,不在andornot内。

我想出了以下定义:

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

虽然这可行,但不是最佳的。我在 andornot 标签中重复了可能的条件。作为副作用,生成的 JAXB 代码是一团糟。

是否有更优雅的方法?

无需多次重复相同的选择 XML Schema 允许您创建全局 xs:group (xs:choice, xs:sequence or 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>