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

发布于 2024-10-20 23:56:24 字数 1577 浏览 5 评论 0原文

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

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

Is it possible to distinct xs:choices in xsd by using fixed values? I have a simple type:

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

And what I want to achieve is

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

When I do this my XMLSpy tells me:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2024-10-27 23:56:24

你不能完全那样做。该错误是因为看到 元素的简单验证器不会立即知道选择哪个分支,而 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>

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

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

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>

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.

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.

倾城月光淡如水﹏ 2024-10-27 23:56:24

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

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

IDs must be unique within a document. You can't use the same value on multiple elements:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文