Xsd 如何使用<;为属性指定唯一约束;xs:unique>;作为<;xs:element>;标签?

Xsd 如何使用<;为属性指定唯一约束;xs:unique>;作为<;xs:element>;标签?,xsd,unique-constraint,Xsd,Unique Constraint,如果我在XSD中有以下元素规范,我可以将约束添加为的子元素,但我无法将其作为的子元素使用: <xs:element name="Parent"> <xs:complexType> <xs:element name="Child" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="Key" type="xs:

如果我在XSD中有以下元素规范,我可以将
约束添加为
的子元素,但我无法将其作为
的子元素使用:

<xs:element name="Parent">
  <xs:complexType>
    <xs:element name="Child" minOccurs="0" maxOccurs="unbounded">
      <xs:complexType>
        <xs:attribute name="Key" type="xs:string" use="required" />
      </xs:complexType>

      <!-- Option A: insert unique constraint here with selector . -->

    </xs:element>
  </xs:complexType>

  <!-- Option B: insert unique constraint here with selector ./Child -->

</xs:element>

这是作为
的子项工作的唯一约束:


但此唯一约束不能作为
的子项使用:



在第二种情况下,是否需要更改选择器XPath?

如果您仔细考虑,选择器“.”将始终返回当前节点;选择器为您提供一个仅包含一个节点的节点集。。。因此,在只有一个节点的集合中,唯一性是有保证的,因为具有给定名称的属性只能发生一次。这应该可以解释为什么你不能以你认为应该的方式得到它

当您在父级设置它时,它会起作用,因为您现在正在一组子节点之间强制唯一性

在数据库术语中,诸如所需内容之类的约束只能在表级别定义。这就是它看起来的样子(我稍微重写了XSD,以便从中获得良好的E/R)

XSD:


另外,我更喜欢对它所针对的元素保持唯一约束,这对我来说似乎更符合逻辑。因此,当您考虑选择器的上下文时,这个问题是完全有意义的。谢谢是否有一种方法可以实现相同的功能,但必须由多个属性组合起来定义唯一状态?。也就是说,在父选择器的上下文中,具有
key1=“X”key2=“Y”和``key1=“X”key2=“Z`的子项被认为是唯一的?或者这更多的是在身份限制的土地上?只是好奇。(和+1,顺便说一句)。
<xs:unique name="ChildKey">
  <xs:selector xpath="./Child"/>
  <xs:field xpath="@Key" />
</xs:unique>
<xs:unique name="ChildKey">
  <xs:selector xpath="."/>
  <xs:field xpath="@Key" />
</xs:unique>
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:tns="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Parent" type="Parent">
        <xs:unique name="ParentKey">
            <xs:selector xpath="tns:Child"/>
            <xs:field xpath="@Key"/>
        </xs:unique>
    </xs:element>
    <xs:complexType name="Parent">
        <xs:sequence>
            <xs:element name="Child" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="Key" type="xs:string" use="required"/>
                </xs:complexType>
                <xs:unique name="ChildKey">
                    <xs:selector xpath="."/>
                    <xs:field xpath="@Key"/>
                </xs:unique>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <Child Key="Key1"/>
    <Child Key="Key1"/>
</Parent>
Error occurred while loading [], line 5 position 3
There is a duplicate key sequence 'Key1' for the 'http://tempuri.org/XMLSchema.xsd:ParentKey' key or unique identity constraint.
Unique.xml is invalid.