0

是否可以通过使用固定值来区分 xsd 中的 xs:choices?我有一个简单的类型:

<xs:simpleType name="datatypeCategory">
    <xs:restriction base="xs:string">
        <xs:enumeration value="SIMPLE"/>
        <xs:enumeration value="COMPLEX"/>
        <xs:enumeration value="COLLECTION"/>
    </xs:restriction>
</xs:simpleType>

而我想要实现的是

<xs:element name="datatype">
    <xs:complexType>
        <xs:choice>
            <xs:sequence>
                <xs:element id="category" type="datatypeCategory" fixed="SIMPLE"/>
                <!-- some fields specific for SIMPLE -->
            </xs:sequence>
            <xs:sequence>
                <xs:element id="category" type="datatypeCategory" fixed="COMPLEX"/>
                <!-- some fields specific for COMPLEX -->
            </xs:sequence>
            <xs:sequence>
                <xs:element id="category" type="datatypeCategory" fixed="COLLECTION"/>
                <!-- some fields specific for COLLECTION -->
            </xs:sequence>
        </xs:choice>
    </xs:complexType>
</xs:element>

当我这样做时,我的 XMLSpy 告诉我:

# The content model of complex type definition '{anonymous}' is ambiguous.
# Details: cos-nonambig: <xs:element name='category'> makes the content model non-deterministic against <xs:element name='category'>. Possible causes: name equality, overlapping occurrence or substitution groups.
4

2 回答 2

1

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

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

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

whereempty被定义为空类型。或者给他们复杂的类型来保存“特定字段”。根据您的约束,还有其他替代方法,例如使用替换组或派生的复杂类型。

但总的来说,XML Schema 1.0 不适合基于相关值的约束。为此,您必须使用 XML Schema 1.1 或外部工具。

于 2011-03-10T01:24:57.543 回答
0

ID 在文档中必须是唯一的。您不能在多个元素上使用相同的值:

http://www.w3.org/TR/2006/REC-xml11-20060816/#id

于 2011-03-09T19:33:12.443 回答