简体   繁体   English

XSD中的唯一元素未强制执行

[英]Unique element in XSD is not enforced

I am trying to create an XSD for a XML and trying to enforce an xs:unique constraint, but it's not enforcing the constraint. 我正在尝试为XML创建XSD并尝试强制执行xs:unique约束,但它没有强制执行约束。 What am I doing wrong? 我究竟做错了什么?

Here is the XSD 这是XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           vc:minVersion="1.1"
           xmlns:o="http://www.osames.org/osamesorm">
  <xs:element name="DATA">
    <xs:annotation>
      <xs:documentation>Element that contain data</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ACCOUNTID" type="xs:string"/>
        <xs:element name="CONTACTS" type="CONTACTType"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="unique-contactid">
      <xs:selector xpath="o:CONTACT"/>
      <xs:field xpath="@ID"/>
    </xs:unique>      
  </xs:element>
  <xs:element name="CONTACT">
    <xs:annotation>
      <xs:documentation>
        Contact Element contain one contact configuration/data
      </xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:all>
        <xs:element name="ID" type="xs:int"/>
        <xs:element name="TITLE" type="xs:string"/>
        <xs:element name="TYPE" type="xs:int"/>
        <xs:element name="CONTACTSTRING" type="xs:string"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="CONTACTType">
    <xs:annotation>
      <xs:documentation>Complex Contect Type</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element ref="CONTACT"  minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

and here is an XML that should be invalid because 2 contacts have same id (0) 这是一个应该无效的XML,因为2个联系人具有相同的id(0)

<?xml version="1.0" encoding="UTF-8"?>
<DATA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="Untitled4.xsd">
    <ACCOUNTID>String</ACCOUNTID>
    <CONTACTS>
        <CONTACT>
            <ID>0</ID>
            <TITLE>String</TITLE>
            <TYPE>0</TYPE>
            <CONTACTSTRING>String</CONTACTSTRING>
        </CONTACT>
        <CONTACT>
            <ID>0</ID>
            <TITLE>String</TITLE>
            <TYPE>0</TYPE>
            <CONTACTSTRING>String</CONTACTSTRING>
        </CONTACT>
    </CONTACTS>
</DATA>

Now sure what am I doing wrong. 现在确定我做错了什么。

Your xs:unique declaration, 你的xs:unique声明,

    <xs:unique name="unique-contactid">
      <xs:selector xpath="o:CONTACT"/>
      <xs:field xpath="@ID"/>
    </xs:unique>                        

has several problems that you must resolve: 有几个问题你必须解决:

  1. CONTACT is not in a namespace; CONTACT不在命名空间中; drop the o: . 放弃o: .
  2. CONTACT is within CONTACTS ; CONTACT方式在CONTACTS方式内; add that element to the XPath. 将该元素添加到XPath。
  3. @ID is not an attribute but an element; @ID不是属性而是元素; change to ID . 更改为ID

Altogether, then, the following xs:unique declaration, 总而言之,以下xs:unique声明,

    <xs:unique name="unique-contactid">
        <xs:selector xpath="CONTACTS/CONTACT"/>
        <xs:field xpath="ID"/>
    </xs:unique>                    

will work as requested. 将按要求工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM