XML Schema:带有可选字段

时间:2015-11-21 16:14:33

标签: xml xsd keyref minoccurs

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

e.g。给出以下架构:

<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>

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

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

我需要minOccurs="0"和keyref约束,如果该字段不存在,则不需要执行任何检查(就像NULL FOREIGN KEYs一样)。

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

1 个答案:

答案 0 :(得分:0)

我希望我能正确理解你的问题。在您的架构中,您需要将<sequence>括在<complexType>中,并且需要关闭指向&#34; subSubElem / @ type&#34;的字段:

<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>

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

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