Unique element in XSD is not enforced

独自空忆成欢 提交于 2021-01-29 02:13:05

问题


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. What am I doing wrong?

Here is the 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 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.


回答1:


Your xs:unique declaration,

    <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; drop the o:.
  2. CONTACT is within CONTACTS; add that element to the XPath.
  3. @ID is not an attribute but an element; change to ID.

Altogether, then, the following xs:unique declaration,

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

will work as requested.



来源:https://stackoverflow.com/questions/38468406/unique-element-in-xsd-is-not-enforced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!