xsd - 如何使用 <xs :unique> as a child element of the <xs:element> tag? 为属性指定唯一约束

标签 xsd unique-constraint

如果我在 XSD 中有以下元素规范,我可以添加 <xs:unique>作为 <xs:element name="Parent"> 的 child 的约束但我不能让它作为 <xs:element name="Child"> 的 child 工作:

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

这是作为 <xs:element name="Parent"> 的 child 的唯一约束。 :
<xs:unique name="ChildKey">
  <xs:selector xpath="./Child"/>
  <xs:field xpath="@Key" />
</xs:unique>

但是这个唯一约束不能作为 <xs:element name="Child"> 的子节点起作用。 :
<xs:unique name="ChildKey">
  <xs:selector xpath="."/>
  <xs:field xpath="@Key" />
</xs:unique>

在第二种情况下我需要更改选择器 XPath 吗?

最佳答案

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

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

在数据库术语中,诸如您需要的约束只能在表级别定义。这就是它的样子(我稍微重写了 XSD 以获得良好的 E/R)。

XSD:

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

XSD图:

enter image description here

等效的 ADO.NET E/R:

enter image description here

显示错误的 XML:
<?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.

关于xsd - 如何使用 <xs :unique> as a child element of the <xs:element> tag? 为属性指定唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8801645/

相关文章:

java - 错误 : Invalid content was found starting with element 'X' . 应为 '{X}' 之一

sql-server - SSIS、带有 XSD 文件的 XML 源...和 ​​nvarchar(max) 目标列

c# - 无法使用 EF 交换两行上的唯一值

python - Flask-sqlalchemy 唯一约束不起作用

PHP - 使用 unicode 正则表达式的 XSD 模式验证错误

c# - 使用强类型 XSD 反序列化 XML 文档时出错

java - JAVA 中的 XSD 验证错误

java - 使用注释的条件唯一约束

java - 在 Hibernate JPA2 上使用唯一约束