XSD - 标签的重用/继承

时间:2016-02-06 16:09:58

标签: xml inheritance xsd jaxb jaxb2

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

<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代码很乱。

对此有更优雅的方法吗?

1 个答案:

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