2016-07-19 8 views
1

XMLのXSDを作成しようとしていて、xs:uniqueという制約を適用しようとしていますが、制約は適用されていません。私は間違って何をしていますか?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> 

であり、ここで2つの接点が同じIDを持っているので、無効であるべきであるXML(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> 

今、私が間違っているのかわから。

答えて

0

あなたxs:unique宣言、

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

はあなたが解決しなければならないいくつかの問題があります。

  1. CONTACTが名前空間ではありませんが。 o:を削除してください。
  2. CONTACTは、CONTACTSの範囲内にあります。その要素をXPathに追加します。
  3. @IDは属性ではなく要素です。 IDに変更してください。全体で

要求されるように、そして、以下のxs:unique宣言、

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

は動作します。

+0

ありがとうございます。完璧に働いた –

関連する問題