2013-02-15 1 views
5

각 책 항목에 대해 고유 ID를 지정해야하는 책에 XML 스키마를 설계하려고합니다. 그러나 그것은 단지 작동하지 않는 것 같습니다. 다음은 내가 사용하고있는 XSD,XML 스키마가 고유 한 중복 ID 허용 여전히

<?xml version="1.0"?> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      elementFormDefault="qualified" 
      attributeFormDefault="unqualified"> 

    <xs:element name="BookShelf"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Description" type="xs:string" minOccurs="0"/> 
     <xs:element name="Shelf" type="ShelfType" minOccurs="1" maxOccurs="10"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

    <xs:complexType name="ShelfType"> 
    <xs:sequence> 
     <xs:element ref="Book" minOccurs="0" maxOccurs="unbounded"/> 
    </xs:sequence> 
    </xs:complexType> 


<xs:element name="Book"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="Title" type="xs:token"/> 
     <xs:element name="Language" type="xs:language"/> 
    </xs:sequence> 
    <xs:attribute name="id" type="xs:string" use="required"/> 
    </xs:complexType> 
    <xs:unique name="unique-bookId"> 
    <xs:selector xpath="Book"/> 
    <xs:field xpath="@id"/> 
    </xs:unique> 
</xs:element> 
</xs:schema> 

나는 이것이으로 검증하기 위해 애 쓰고 XML, 심지어 안하지만 잘 검증되어

<?xml version="1.0"?> 

<BookShelf> 
    <Description>My bookshelf</Description> 
    <Shelf> 
     <Book id="1"> 
      <Title>Seitsemän veljestä</Title> 
      <Language>fi</Language> 
     </Book> 
     <Book id="1"> 
      <Title>Another title</Title> 
      <Language>en</Language> 
     </Book> 
    </Shelf> 
</BookShelf> 

는 (I는 동일한 ID를 사용했습니다입니다 2 항목). XML을 처음 접했을 때 누군가 내가 여기서 잘못하고있는 것을 지적 해 주시면 감사하겠습니다.

답변

10

당신은 잘못된 장소에서 <xs:unique>있어 - 그것은 Book 요소가 아니라 Book 요소 정의 그 자체로 고유해야하는 내 조상 요소의 정의 안에 있어야합니다. 다음은 각 선반 내에서 고유해야 할 책 ID를 강제하지만, 다른 선반에 같은 ID 수있는 것입니다 : 모든 선반은 다음에 고유 제한 조건을 넣어 걸쳐 ID를 전 세계적으로 고유하려면 대신하는 경우

<xs:element name="BookShelf"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Description" type="xs:string" minOccurs="0"/> 
     <xs:element name="Shelf" type="ShelfType" minOccurs="1" maxOccurs="10"> 
      <xs:unique name="unique-bookId"> 
      <xs:selector xpath="Book"/><!-- selects books on this shelf --> 
      <xs:field xpath="@id"/> 
      </xs:unique> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

BookShelf 수준 적절하게 선택을 조정 : 향후 참조를 위해

<xs:element name="BookShelf"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Description" type="xs:string" minOccurs="0"/> 
     <xs:element name="Shelf" type="ShelfType" minOccurs="1" maxOccurs="10"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:unique name="unique-bookId"> 
     <xs:selector xpath="Shelf/Book"/><!-- selects books on all shelves --> 
     <xs:field xpath="@id"/> 
    </xs:unique> 
    </xs:element> 

는 스키마가 targetNamespace이 있다면있는 그대로 선택 XPath의에서 접두어가 이름은 항상 "네임 스페이스"를 의미하지 않기 때문에 다음 해당 선택기가 작동하지 않을 있습니다. xs:schema 요소에 xmlns:tns="<target namespace URI>"을 추가 한 다음 tns:Shelf/tns:Book의 선택자를 사용해야합니다.

3

구체적으로 숫자를 ID 값으로 사용해야합니까?

<xs:attribute name="id" type="xs:ID" use="required"/> 

그 방법은 unique XML identifiersid 속성의 값으로 허용되는 다음 id 대신 xs:string의 입력 속성으로하지 않으면 하나의 가능성은 xs:ID을 사용하고 있습니다. 유효한 XML 식별자는 숫자로 시작할 수 없으므로 ID 유형을 id-1과 같이 변경해야합니다.