Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSD中的可选键_Xsd_Key - Fatal编程技术网

XSD中的可选键

XSD中的可选键,xsd,key,Xsd,Key,我在根元素上创建了一个key/keyref,以便基于指定的元素创建文档范围的唯一性 因此,通过//foo/@name在foo的所有实例中出现的@name必须是唯一的;对于//bar/@name也是如此。这似乎很有效。它们分别由//foo-ref/@name-ref和//bar-ref/@name-ref引用,也在根节点上定义 然而,我发现不能创建可选密钥,这带来了一点问题。从语义上讲,根据符合性文件的性质,并非每个foo或bar实例都需要密钥。foo-ref/@name-ref的实例显然需要针对

我在根元素上创建了一个
key
/
keyref
,以便基于指定的元素创建文档范围的唯一性

因此,通过
//foo/@name
foo
的所有实例中出现的
@name
必须是唯一的;对于
//bar/@name
也是如此。这似乎很有效。它们分别由
//foo-ref/@name-ref
//bar-ref/@name-ref
引用,也在根节点上定义

然而,我发现不能创建可选密钥,这带来了一点问题。从语义上讲,根据符合性文件的性质,并非每个
foo
bar
实例都需要密钥。
foo-ref/@name-ref
的实例显然需要针对现有的
foo/@name
,但是
foo
没有
@name
在语义上并不无效

这方面有什么办法吗?我不喜欢消费者必须为每一个元素定义一个键的想法,因为合理地说,只有少数人需要它们

下面是示例模式(当然,我没有部署一些
foobar
schema,但是结构是相同的;这只是我一直在玩的一个测试模式)

使用;与键不同,其匹配值是唯一的或nil(nil或not present)

例如:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="uk" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:attribute name="name" type="xsd:string"/>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="fk" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:attribute name="name" type="xsd:string"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:unique name="uq">
            <xsd:selector xpath="uk"/>
            <xsd:field xpath="@name"/>
        </xsd:unique>
        <xsd:keyref name="fk" refer="uq">
            <xsd:selector xpath="fk"/>
            <xsd:field xpath="@name"/>
        </xsd:keyref>
    </xsd:element>
</xsd:schema>

示例(有效)XML:



unique
不会保持引用完整性,是吗?我可以通过
keyref
引用
unique
吗?哇。这非常简单;我不知何故认为keyrefs必须引用键。这让生活变得更容易:)@Bracketworks,事实上我发现,网站上的许多有用的帖子误导了人们,让他们相信一个keyref不能引用unique,例如。其他网站也暗示了同样的意思……完全一样;同样,我也被误导了(主要是被其他资源误导了),再次感谢,这困扰了我一段时间(包括在以前的模式中,我甚至记不起我使用了什么作为解决方案;我可能只是放弃了这个验证因素)
<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="foo" type="foo" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
    <xs:keyref name="foo-ref" refer="foo">
        <xs:selector xpath=".//foo-ref" />
        <xs:field xpath="@name-ref" />
    </xs:keyref>
    <xs:keyref name="bar-ref" refer="bar">
        <xs:selector xpath=".//bar-ref" />
        <xs:field xpath="@name-ref" />
    </xs:keyref>
    <!--
        the use of xs:unique here, in lieu of xs:key allows for
        nullable "keys", retaining referential integrity with the
        above defined keyrefs. awesome possum.
    -->
    <xs:unique name="foo">
        <xs:selector xpath=".//foo" />
        <xs:field xpath="@name" />
    </xs:unique>
    <xs:unique name="bar">
        <xs:selector xpath=".//bar" />
        <xs:field xpath="@name" />
    </xs:unique>
</xs:element>
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="uk" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:attribute name="name" type="xsd:string"/>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="fk" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:attribute name="name" type="xsd:string"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:unique name="uq">
            <xsd:selector xpath="uk"/>
            <xsd:field xpath="@name"/>
        </xsd:unique>
        <xsd:keyref name="fk" refer="uq">
            <xsd:selector xpath="fk"/>
            <xsd:field xpath="@name"/>
        </xsd:keyref>
    </xsd:element>
</xsd:schema>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <uk name="name1"/>
    <uk />
    <fk/>
    <fk name="name1"/>
</root>