2016-08-15 3 views
1

재미있는 목적으로 프로젝트 변경 내용을 저장하고 싶은 XML 파일을 만듭니다. 따라서 저는 작은 XML 스키마를 작성했습니다. 문제는 각 Revisionid 속성이 고유해야한다는 것입니다.xsd : unique 사용하는 방법?

그래서 인터넷과 스택 오버플로를 통해 검색했지만이 문제를 해결할 수 없습니다. 나는 Visual Studio 2015를 사용하고 있습니다. 이것이 문제가되는지 확실하지 않습니다.

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/XMLSchema.xsd" 
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 
    <xs:element name="Revisions"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Revision" maxOccurs="unbounded"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="Author" type="xs:string"/> 
          <xs:element name="Date" type="xs:date"/> 
          <xs:element name="Comments" type="xs:string"/> 
         </xs:sequence> 
         <xs:attribute name="id" type="xs:string" use="required"/> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
     <xs:unique name="Revision"> 
      <xs:annotation> 
       <xs:documentation> 
        The id of each Revision element must be unique. 
       </xs:documentation> 
      </xs:annotation> 
      <xs:selector xpath="Revision"/> 
      <xs:field xpath="@id"/> 
     </xs:unique> 
    </xs:element> 
</xs:schema> 

그리고 XML 파일 내가 사용하고 그 : 내가 사용

XML 스키마는

<?xml version="1.0" encoding="utf-8"?> 
<Revisions xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Revision id="1"> 
     <Author>JP</Author> 
     <Date>2014-07-09</Date> 
     <Comments>Initial version</Comments> 
    </Revision> 
    <Revision id="2"> 
     <Author>JP</Author> 
     <Date>2016-01-26</Date> 
     <Comments> 
      Created a RegistrationValidator class which uses regular expressions to check 
      if usernames, passwords, email addresses, etc.. are in correct format 
     </Comments> 
    </Revision> 
    <Revision id="3"> 
     <Author>JP</Author> 
     <Date>2016-08-14</Date> 
     <Comments>Created an XML schema which validates this XML file</Comments> 
    </Revision> 
    <Revision id="3"> 
     <Author>Test</Author> 
     <Date>2016-08-14</Date> 
     <Comments>dummy comments</Comments> 
    </Revision> 
</Revisions> 

당신은 내가 같은 id 값을 할당했습니다 볼 수 있듯이 마지막으로 Revision 태그로 보내면 오류 목록에서 오류, 경고 또는 메시지가 표시되지 않습니다. 누군가 내가 잘못하고있는 것을 알고 있습니까?

+0

일부 관련 질문은 여기에 있습니다 : http://stackoverflow.com/questions/5541305/xml-xsd-schema-enforce-unique 그냥 문제를 해결하기 위해 xs:selector/@xpath에, 네임 스페이스 접두사, mstns를 추가 -attribute-values-in-schema http://stackoverflow.com/questions/15509504/xml-schema-add-unique-id-for-several-child-elements http://stackoverflow.com/questions/14893435/xml -schema-still-allows-duplicate-ids-with-unique – SilverSkin

답변

1

매우 가까웠습니다.

<xs:unique name="Revision"> 
    <xs:annotation> 
    <xs:documentation> 
     The id of each Revision element must be unique. 
    </xs:documentation> 
    </xs:annotation> 
    <xs:selector xpath="mstns:Revision"/> 
    <xs:field xpath="@id"/> 
</xs:unique>