XSD 对同一元素的多个实例的 keyref 约束

XSD keyref contraints on multiple instances of same element

架构示例 -

<xs:complexType name="EndPointType">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="Pass" type="SIPMethodType" minOccurs="0" />
        <!-- other elements -->
        <xs:choice>
            <xs:element name="Method" type="MethodType" minOccurs="0" maxOccurs="1"/>
            <xs:element name="MethodName" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
        </xs:choice>
    </xs:choice>
</xs:complexType>

<xs:complexType name="MethodType">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="Method" type="SIPMethodType" minOccurs="1" maxOccurs="1" />
        <xs:element name="Response" type="SIPResponseCode" minOccurs="0" maxOccurs="1" />
        <xs:element name="Direction" type="DirectionType" minOccurs="0" maxOccurs="4"/>
    </xs:choice>
</xs:complexType>

<xs:element name="SSME">
    <xs:complexType>
        <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded" >
                <xs:element name="Endpoint" type="EndPointType" minOccurs="1" maxOccurs="unbounded" />
                <xs:element name="Method" type="MethodType" minOccurs="0" maxOccurs="unbounded" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="MethodNameKey">
        <xs:selector xpath="./Method"/>
        <xs:field xpath="Name"/>
    </xs:key>
    <xs:keyref name="EpMethodKeyRef"  refer="MethodNameKey">
        <!-- endpoint->methodName (if present) must point at an existing method->name -->
        <xs:selector xpath="./Endpoint"/>
        <xs:field xpath="MethodName"/>
    </xs:keyref>
</xs:element>

xml 有问题 -

<Endpoint>
    <Name>ep1</Name>
    <MethodName>method1</MethodName>
    <MethodName>method2</MethodName>
</Endpoint> 

<Method>
    <Name>method1</Name>
    <Method>CANCEL</Method>
    <Direction>Outbound</Direction>
</Method>   

<Method>
    <Name>method2</Name>
    <Method>INVITE</Method>
    <Direction>Inbound</Direction>
</Method>   

xmllint 给我 - 元素方法名称:模式有效性错误:元素 'MethodName':keyref 身份约束 'EpMethodKeyRef' 字段的 XPath 'MethodName' 评估为具有多个成员的节点集。

如何指定 EpMethodKeyRef keyref 以允许 MethodName 的 0-n 个实例,每个实例必须包含一个已在 MethodNameKey 中定义的方法? 我试过了

<xs:field xpath="MethodName[0]"/>
<xs:field xpath="MethodName[1]"/>
<xs:field xpath="MethodName[*]"/>

xs:selector 必须 select 每个元素都有一个外键引用,而 xs:field 指向每个元素的引用。

MethodName 应移至 xs:selector,xs:field 应具有上下文项。反而。这样,每个元素 MethodName 将被原子化以获取其值并将其与键进行比较,就像元素 Name 在键声明中被原子化一样。

    <xs:keyref name="EpMethodKeyRef"  refer="MethodNameKey">
        <!-- endpoint->methodName (if present) must point at an existing method->name -->
        <xs:selector xpath="./Endpoint/MethodName"/>
        <xs:field xpath="."/>
    </xs:keyref>

多次使用具有相同 select 的 xs:field 或具有创建元组键的不同语义。