2011-10-10 14 views
10

Powiedzmy mam następujący plik XML:UNIQUE w XML Schema

<authors> 
    <author>a1</author> 
    <author>a2</author> 
    <lastmodified>2010</lastmodified> 
</authors> 

i fragment schematu XML:

<xs:element name="authors" maxOccurs="1"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="author" maxOccurs="unbounded" type="xs:string"> </xs:element> 
     <xs:element name="lastmodified" type="xs:date" minOccurs="0"/> 
    </xs:sequence> 
    </xs:complexType> 
    <xs:unique name="uniqueAuthor"> 
    <xs:selector xpath="."/> 
    <xs:field xpath="author"/> 
    </xs:unique> 
</xs:element> 

co chcę zrobić ograniczenie, które nie pozwolą dwa identyczne wartości autora, ale powyższe nie działa w ten sposób. Co ja robię źle?

Odpowiedz

16

XPath wybiera węzły, które muszą być unikatowe (w takim przypadku powinien wybrać węzły autora).

XPath wybiera, co "czyni je unikalnymi" (w takim przypadku użycie . spowoduje ich wpisaną wartość, w tym przypadku tekst między tagami, traktowany jako ciąg znaków, do użycia).

Dokument

<?xml version="1.0" encoding="UTF-8"?> 
<authors> 
    <author>a1</author> 
    <author>a2</author> 
    <lastmodified>2010-01-01</lastmodified> 
</authors> 

powinien być ważny na poniższym schemacie:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="authors"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="author" maxOccurs="unbounded" type="xs:string"/> 
     <xs:element name="lastmodified" type="xs:date" minOccurs="0"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:unique name="uniqueAuthor"> 
     <xs:selector xpath="author"/> 
     <xs:field xpath="."/> 
    </xs:unique> 
    </xs:element> 
</xs:schema> 

podczas gdy ten nie powinien:

<?xml version="1.0" encoding="UTF-8"?> 
<authors> 
    <author>a1</author> 
    <author>a1</author> 
    <lastmodified>2010-01-01</lastmodified> 
</authors> 
1

Możesz użyć type = "xs: ID" na elemencie autor. Istnieje również IDREF dla określenia ID.

+0

wyjątkowość ograniczenia mają pewne zalety w stosunku 'xs: ID ', zobacz http://www.xml.com/pub/a/2002/11/20/schemas.html#identity_constraints – DaveFar

Powiązane problemy