XSD复杂类型继承中的限制问题

问题描述

下面的xsd有点问题。我创建了具有类型属性的保管库对象,该属性可以在枚举中具有任何值。然后,我从vaultobject派生了VaultServiceObject,并设置了一个限制,以将派生对象的类型限制为固定值。

编辑器(liquid-xml)设计服务似乎理解了这一点并正确显示,但是文本编辑器将第26行标记为和错误

<xs:attribute name="Type" fixed="ServiceConfiguration" type="xs:string" use="required">

说“错误无效的属性限制。派生的属性类型不是对基本属性类型的有效限制。”因此,我认为“ xs:string”是错误的。但是我不知道应该使用哪种类型。

希望那里有人对XSD更有经验。

p.s我从其他几个类似的stackoverflow问题中整理了下面的xsd,但是它们没有提供这种确切的组合及其解决方案。因此,请不要在没有解释我要寻找的内容的情况下指向那些人。

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="VaultObject">
        <xs:sequence>
            <xs:annotation>
                <xs:documentation>Allows for derived object to have a sequence of elements</xs:documentation>
            </xs:annotation>
        </xs:sequence>
        <xs:attribute name="Type" use="required">
            <xs:annotation>
                <xs:documentation>This is the list of possible vault objects. Derived objects need to lock this down to the object type the represent.</xs:documentation>
            </xs:annotation>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Merge" />
                    <xs:enumeration value="ServiceConfiguration" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:anyAttribute />
    </xs:complexType>
    <xs:complexType name="VaultServiceConfigurationObject">
        <xs:complexContent>
            <xs:restriction xmlns:q2="http://www.it-workz.nl/IDM" base="VaultObject">
                <xs:attribute name="Type" fixed="ServiceConfiguration" type="xs:string" use="required">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">This property is inherited from VaultObject,but is locked down to the fixed value of "ServiceConfiguration"</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="ServiceType">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">The list of possible service types we support. Derived service deFinitions need to lock this down to a single value in their own type.</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="SystemA" />
                            <xs:enumeration value="SystemB" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
                <xs:attribute name="Name" type="xs:string" use="required">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">Every service needs to be uniquely named. Even between different service types.</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

解决方法

您可以通过将“类型”枚举分解为它自己的SimpleType来实现。

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="VaultObjectType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Merge" />
            <xs:enumeration value="ServiceConfiguration" />
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="VaultObject">
        <xs:sequence>
            <xs:annotation>
                <xs:documentation>Allows for derived object to have a sequence of elements</xs:documentation>
            </xs:annotation>
        </xs:sequence>
        <xs:attribute name="Type" type="VaultObjectType" use="required">
            <xs:annotation>
                <xs:documentation>This is the list of possible vault objects. Derived objects need to lock this down to the object type the represent.</xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:anyAttribute />
    </xs:complexType>
    <xs:complexType name="VaultServiceConfigurationObject">
        <xs:complexContent>
            <xs:restriction xmlns:q2="http://www.it-workz.nl/IDM" base="VaultObject">
                <xs:attribute name="Type" fixed="ServiceConfiguration" type="VaultObjectType" use="required">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">This property is inherited from VaultObject,but is locked down to the fixed value of "ServiceConfiguration"</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="ServiceType">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">The list of possible service types we support. Derived service definitions need to lock this down to a single value in their own type.</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="SystemA" />
                            <xs:enumeration value="SystemB" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
                <xs:attribute name="Name" type="xs:string" use="required">
                    <xs:annotation>
                        <xs:documentation xml:lang="EN">Every service needs to be uniquely named. Even between different service types.</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>