仅在订单指示器中的唯一元素

Unique Element only in Order Indicator

有没有办法让元素在同一序列(选择,所有)中唯一,但不是在整个文档中。

例如这应该有效(John 和 Jane 的中间名都是 Francis):

<students xsi:noNamespaceSchemaLocation="stack.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <student>
        <name>John</name>
        <middlename>Francis</middlename>
        <middlename>Mike</middlename>
        <lastname>Doe</lastname>
    </student>
    <student>
        <name>Jane</name>
        <middlename>Francis</middlename>
        <lastname>Doe</lastname>
    </student>
</students>

但这应该行不通(约翰有两个相似的中间名):

<students xsi:noNamespaceSchemaLocation="stack.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <student>
        <name>John</name>
        <middlename>Francis</middlename>
        <middlename>Francis</middlename>
        <lastname>Doe</lastname>
    </student>
</students>

到目前为止,我已经尝试关注我的 .xsd 文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="students">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="student">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:sequence minOccurs="0" maxOccurs="5">
                                <xs:element name="middlename" type="xs:string"/>
                            </xs:sequence>
                            <xs:element name="lastname" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        
        <xs:unique name="middlename_unique">
            <xs:selector xpath="student/middlename"/>
            <xs:field xpath="."></xs:field>
        </xs:unique>
    </xs:element>
</xs:schema>

但这只会让中间名变得独一无二,而不是对单个学生而言...

https://www.w3.org/TR/xmlschema-0/#specifyingUniqueness

XML Schema enables us to indicate that any attribute or element value must be unique within a certain scope. To indicate that one particular attribute or element value is unique, we use the unique element first to "select" a set of elements, and then to identify the attribute or element "field" relative to each selected element that has to be unique within the scope of the set of selected elements.

所以我想你想要

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="students">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="student">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:sequence minOccurs="0" maxOccurs="5">
                                <xs:element name="middlename" type="xs:string"/>
                            </xs:sequence>
                            <xs:element name="lastname" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                    <xs:unique name="middlename_unique">
                        <xs:selector xpath="middlename"/>
                        <xs:field xpath="."/>
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>