且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过对枚举类型的元素使用固定值来区分xs:xsd中的选择

更新时间:2023-10-06 23:08:16

您不能完全做到这一点。该错误是因为看到< category> 元素的简单验证器不会立即知道选择哪个分支,并且XML Schema 1.0支持这种简单的验证器。

You can't do exactly that. The error is because a simple validator that sees a <category> element won't immediately know which branch of the choice to take, and XML Schema 1.0 supports such simple validators.

另一种方法是根据类别来命名每个元素。

An alternative would be to name each element according to the category.

<xs:element name="datatype">
    <xs:complexType>
        <xs:choice>
            <xs:sequence>
                <xs:element name="simpleCategory" type="empty"/>
                <!-- some fields specific for SIMPLE -->
            </xs:sequence>
            <xs:sequence>
                <xs:element name="complexCategory" type="empty"/>
                <!-- some fields specific for COMPLEX -->
            </xs:sequence>
            <xs:sequence>
                <xs:element name="collectionCategory" type="empty"/>
                <!-- some fields specific for COLLECTION -->
            </xs:sequence>
        </xs:choice>
    </xs:complexType>
</xs:element>

其中 empty 被定义为空类型。或者给他们复杂的类型来保存特定字段。还有其他一些选择,具体取决于您的约束,例如使用替换组或派生的复杂类型。

where empty is defined as an empty type. Or give them complex types to hold the "specific fields". There are other alternatives depending on your constraints, such as using substitution groups or derived complex types.

尽管如此,总的来说,XML Schema 1.0不适用于基于相关值的约束。为此,您必须转到XML Schema 1.1或外部工具。

In general though, XML Schema 1.0 is not good for constraints based on interrelated values. For that, you have to go to XML Schema 1.1 or an external tool.