XSD "one or both" 选择不适用于类型限制

XSD "one or both" choice does not work with type restriction

我想创建一个 XSD,其中一个项目需要指定至少一个标识符,或同时指定两个标识符,但从不 none。 这在第一时间工作正常:

<xs:choice>
<xs:sequence>
    <xs:element name="OneIdType">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:maxLength value="50"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="AnotherIdType" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:element name="AnotherIdType" type="xs:string"/>

但是两种 IdType 都应该有(不同的)长度限制。 因此,当我尝试以下操作时,我的 XSD 无效:

<xs:choice>
<xs:sequence>
    <xs:element name="OneIdType">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:maxLength value="50"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="AnotherIdType" minOccurs="0">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:maxLength value="20"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
</xs:sequence>
<xs:element name="AnotherIdType">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:maxLength value="20"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

我收到错误

Element ' AnotherIdType ' is not consistent with element ' AnotherIdType ' .

那么在 XSD 中是否有可能实现具有受限类型的 "one or both choice"?

谢谢, 最大值

尽管这两个 AnotherIdType 元素声明中的匿名类型具有相同的定义,但它们不是 相同的类型 。拉出simpleType声明到顶层

<xs:simpleType name="max20String">
    <xs:restriction base="xs:string">
        <xs:maxLength value="20"/>
    </xs:restriction>
</xs:simpleType>

然后从两个元素中引用它

<xs:choice>
<xs:sequence>
    <xs:element name="OneIdType">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:maxLength value="50"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="AnotherIdType" type="tns:max20String" minOccurs="0"/>
</xs:sequence>
<xs:element name="AnotherIdType" type="tns:max20String"/>

(其中 tns 前缀绑定到架构的 targetNamespace