如何使用 &lt;xs:unique&gt; 为属性指定唯一约束?作为 <xs:element> 的子元素标签? [英] How can I specify a unique constraint for attributes using the &lt;xs:unique&gt; as a child element of the &lt;xs:element&gt; tag?

查看:36
本文介绍了如何使用 &lt;xs:unique&gt; 为属性指定唯一约束?作为 <xs:element> 的子元素标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 XSD 中有以下元素规范,我可以添加 约束作为 但我无法让它作为 <xs:element name="Child"> 的孩子工作:

If I have the following element spec in XSD I can add the <xs:unique> constraint as a child of <xs:element name="Parent"> but I can't get it to work as a child of <xs:element name="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"> 的孩子的唯一约束:

This is the unique constraint that works as a child of <xs:element name="Parent">:

<xs:unique name="ChildKey">
  <xs:selector xpath="./Child"/>
  <xs:field xpath="@Key" />
</xs:unique>

但是这个唯一约束不能作为 <xs:element name="Child"> 的子项:

But this unique constraint doesn't work as a child of <xs:element name="Child">:

<xs:unique name="ChildKey">
  <xs:selector xpath="."/>
  <xs:field xpath="@Key" />
</xs:unique>

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

Do I need to change the selector XPath in the second case?

推荐答案

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

If you think about it, the selector "." will always return the current node; the selector gives you a node set with one node only... So, within a set with one node only, the uniqueness is guaranteed, since an attribute with a given name can only happen once. This should explain why you cannot get it the way you think it should work.

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

When you set it at the parent level, it works because you are now enforcing uniqueness among a set of Child nodes.

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

In database terms, a constraint such as what you need can only be defined at the table level. This is what it would look like (I've rewritten the XSD slightly to get a good E/R out of it).

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

等效的 ADO.NET E/R:

The equivalent ADO.NET E/R:

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

这篇关于如何使用 &lt;xs:unique&gt; 为属性指定唯一约束?作为 <xs:element> 的子元素标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆