XML Schema:带有可选字段的keyref

时间:2021-07-05 17:18:05

I wonder if it's possible to use keyref element with optional fields.

我想知道是否可以将keyref元素与可选字段一起使用。

e.g. Given the following schema:

例如给定以下架构:

<element name="myElement" type="myType">
    <complexType>
        <sequence>
            <element name="subElem">
                <complexType>
                    <attribute name="name" type="string"/>
                    <attribute name="type" type="string"/>
                </complexType>
            </element>
            <element name="subElem2">
                <complexType>
                    <sequence>
                        <element name="subSubElem" minOccurs="0">
                            <complexType>
                                <attribute name="type" type="string"/>
                            </complexType>
                        </element>
                    </sequence>
                    <attribute name="name" type="string" use="required"/>
                </complexType>
            </element>
        </sequence>
    </complexType>
    <key name="uniqueSubElem">
        <selector xpath="subElem"/>
        <field xpath="@name"/>
        <field xpath="@type"/>
    </key>
    <keyref name="uniqueSubElemRef" refer="uniqueSubElem">
        <selector xpath="subElem2"/>
        <field xpath="@name"/>
        <field xpath="subSubElem/@type"/>
    </keyref>
</element>

In this example if I don't specify a subSubElem in the XML the validator will complain:

在这个例子中,如果我没有在XML中指定subSubElem,验证器会抱怨:

Not enough values specified for <keyref name="uniqueSubElem"> identity constraint specified for element "myElement".

I need both the minOccurs="0" and the keyref constraint, and if the field is not present simply don't want any check to be performed (as for NULL FOREIGN KEYs).

我需要minOccurs =“0”和keyref约束,如果该字段不存在,则根本不希望执行任何检查(对于NULL FOREIGN KEY)。

EDIT: Ignore namespaces for this example, I used default namespace for everything...

编辑:忽略此示例的命名空间,我使用默认命名空间...

1 个解决方案

#1


0  

I hope I understood your problem correctly. In your schema, you need to enclose <sequence> in <complexType>, and you need to close the field pointing to "subSubElem/@type":

我希望我能正确理解你的问题。在模式中,需要在 中包含 ,并且需要关闭指向“subSubElem / @ type”的字段:

<xs:element name="myElement">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="subElem">
                <xs:complexType>
                    <xs:attribute name="name" type="xs:string"/>
                    <xs:attribute name="type" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="subElem2">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="subSubElem">
                            <xs:complexType>
                                <xs:attribute name="type" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="uniqueSubElem">
        <xs:selector xpath="subElem"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="@type"/>
    </xs:key>
    <xs:keyref name="uniqueSubElemRef" refer="uniqueSubElem">
        <xs:selector xpath="subElem2"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="subSubElem/@type"/>
    </xs:keyref>
</xs:element>

This XML should validate without specifying a subSubElem:

此XML应在不指定subSubElem的情况下进行验证:

<myElement>
    <subElem name="foo" type="bar"/>
    <subElem2 name="foo2"/>
</myElement>

#1


0  

I hope I understood your problem correctly. In your schema, you need to enclose <sequence> in <complexType>, and you need to close the field pointing to "subSubElem/@type":

我希望我能正确理解你的问题。在模式中,需要在 中包含 ,并且需要关闭指向“subSubElem / @ type”的字段:

<xs:element name="myElement">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="subElem">
                <xs:complexType>
                    <xs:attribute name="name" type="xs:string"/>
                    <xs:attribute name="type" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="subElem2">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="subSubElem">
                            <xs:complexType>
                                <xs:attribute name="type" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="uniqueSubElem">
        <xs:selector xpath="subElem"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="@type"/>
    </xs:key>
    <xs:keyref name="uniqueSubElemRef" refer="uniqueSubElem">
        <xs:selector xpath="subElem2"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="subSubElem/@type"/>
    </xs:keyref>
</xs:element>

This XML should validate without specifying a subSubElem:

此XML应在不指定subSubElem的情况下进行验证:

<myElement>
    <subElem name="foo" type="bar"/>
    <subElem2 name="foo2"/>
</myElement>