2012-10-18 4 views
1

J'essaie de créer un élément unique. Voici une partie de mon code.Élément unique XS-Schema

<complexType name="DeliveryOrder"> 
    <sequence> 
     <element name="Depot" type="tns:County" minOccurs="1" maxOccurs="1"></element> 
     <element name="CustomerDetails" type="tns:CustomerDetails" minOccurs="1" maxOccurs="1"></element> 
     <element name="ItemsToBeDelivered" 
      type="tns:ItemsToBeDelivered" maxOccurs="unbounded" minOccurs="1"> 
     </element> 
    </sequence> 
</complexType> 

<complexType name="CustomerDetails"> 
    <sequence> 
     <element name="FirstName"> 
      <simpleType> 
       <restriction base="string"> 
        <pattern value="[a-zA-Z][a-z][a-z]*"></pattern> 
       </restriction> 
      </simpleType> 
     </element> 
     <element name="Surname"> 
      <simpleType> 
       <restriction base="string"> 
        <pattern value="[a-z{^}A-Z][a-z][a-z]*"></pattern> 
       </restriction> 
      </simpleType> 
     </element> 
     <element name="AddressLine1"> 
      <simpleType> 
       <restriction base="string"> 
        <minLength value="4"></minLength> 
        <whiteSpace value="collapse"></whiteSpace> 
       </restriction> 
      </simpleType> 
     </element> 
     <element name="AddressLine2"> 
      <simpleType> 
       <restriction base="string"> 
        <minLength value="4"></minLength> 
        <whiteSpace value="collapse"></whiteSpace> 
       </restriction> 
      </simpleType> 
     </element> 
     <element name="AddressLine3" type="string"> 
     </element> 
     <element name="County" type="tns:County" maxOccurs="1" minOccurs="1"></element> 
    </sequence> 
</complexType> 

<complexType name="County"> 
    <choice> 
     <element name="CountyName"> 
      <simpleType> 
       <restriction base="string"> 
        <enumeration value="Dublin"></enumeration> 
        <enumeration value="Cork"></enumeration> 
        <enumeration value="Kildare"></enumeration> 
        <enumeration value="Waterford"></enumeration> 
        <enumeration value="Wicklow"></enumeration> 
       </restriction> 
      </simpleType> 
     </element> 
    </choice> 
</complexType> 

<complexType name="DaysForDelivery"> 
    <sequence> 
     <element name="Day"> 
      <simpleType> 
       <restriction base="string"> 
        <enumeration value="Monday"></enumeration> 
        <enumeration value="Tuesday"></enumeration> 
        <enumeration value="Wednesday"></enumeration> 
        <enumeration value="Thursday"></enumeration> 
        <enumeration value="Friday"></enumeration> 
       </restriction> 
      </simpleType> 
     </element> 
    </sequence> 
</complexType> 

<element name="DeliveryOrder" type="tns:DeliveryOrder"></element> 

<complexType name="ItemsToBeDelivered"> 
    <sequence> 
     <element name="DaysForDelivery" type="tns:DaysForDelivery" 
      maxOccurs="unbounded" minOccurs="1"> 
     </element> 
     <element name="Item" type="tns:Item" maxOccurs="unbounded" minOccurs="1"></element> 
    </sequence> 
</complexType> 

<complexType name="Item"> 
    <sequence> 
     <element name="Description"> 
      <simpleType> 
       <restriction base="string"> 
        <enumeration 
         value="Regular Milk - 1 litre carton."> 
        </enumeration> 
        <enumeration 
         value="Low Fat Milk - 1 litre carton."> 
        </enumeration> 
        <enumeration 
         value="Strawberry Yoghurt - 100ml pot"> 
        </enumeration> 
        <enumeration 
         value="Raspberry Yoghurt - 100ml pot"> 
        </enumeration> 
        <enumeration value="Cream - 125ml carton"></enumeration> 
        <enumeration value="Cream - 250ml carton"></enumeration> 
       </restriction> 
      </simpleType> 
     </element> 
     <element name="Quantity"> 
      <simpleType> 
       <restriction base="int"> 
        <minInclusive value="1"></minInclusive> 
       </restriction> 
      </simpleType> 
     </element> 
    </sequence> 
</complexType> 

Donc, je veux l'élément "jour" de "DaysForDelivery" d'être unique. par exemple: Je peux avoir un lundi un objet X, et après cela ne plus pouvoir utiliser lundi. Je sais que je dois utiliser la balise unique, et j'ai jeté un coup d'oeil à de nombreux exemples, mais je le gâcher avec les xpaths.

Nous vous remercions à l'avance

Répondre

1

À JOUR: Voici le XSD modifié:

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:tns="http://tempuri.org/XMLSchema.xsd" xmlns="http://www.w3.org/2001/XMLSchema"> 
    <complexType name="DeliveryOrder"> 
     <sequence> 
      <element name="Depot" type="tns:County" minOccurs="1" maxOccurs="1"></element> 
      <element name="CustomerDetails" type="tns:CustomerDetails" minOccurs="1" maxOccurs="1"></element> 
      <element name="ItemsToBeDelivered" type="tns:ItemsToBeDelivered" maxOccurs="unbounded" minOccurs="1"> 
       <unique name="UQ"> 
        <selector xpath="tns:DaysForDelivery/tns:Day"/> 
        <field xpath="."/> 
       </unique> 
      </element> 
     </sequence> 
    </complexType> 
    <complexType name="CustomerDetails"> 
     <sequence> 
      <element name="FirstName"> 
       <simpleType> 
        <restriction base="string"> 
         <pattern value="[a-zA-Z][a-z][a-z]*"></pattern> 
        </restriction> 
       </simpleType> 
      </element> 
      <element name="Surname"> 
       <simpleType> 
        <restriction base="string"> 
         <pattern value="[a-z{^}A-Z][a-z][a-z]*"></pattern> 
        </restriction> 
       </simpleType> 
      </element> 
      <element name="AddressLine1"> 
       <simpleType> 
        <restriction base="string"> 
         <minLength value="4"></minLength> 
         <whiteSpace value="collapse"></whiteSpace> 
        </restriction> 
       </simpleType> 
      </element> 
      <element name="AddressLine2"> 
       <simpleType> 
        <restriction base="string"> 
         <minLength value="4"></minLength> 
         <whiteSpace value="collapse"></whiteSpace> 
        </restriction> 
       </simpleType> 
      </element> 
      <element name="AddressLine3" type="string"> 
      </element> 
      <element name="County" type="tns:County" maxOccurs="1" minOccurs="1"></element> 
     </sequence> 
    </complexType> 

    <complexType name="County"> 
     <choice> 
      <element name="CountyName"> 
       <simpleType> 
        <restriction base="string"> 
         <enumeration value="Dublin"></enumeration> 
         <enumeration value="Cork"></enumeration> 
         <enumeration value="Kildare"></enumeration> 
         <enumeration value="Waterford"></enumeration> 
         <enumeration value="Wicklow"></enumeration> 
        </restriction> 
       </simpleType> 
      </element> 
     </choice> 
    </complexType> 

    <complexType name="DaysForDelivery"> 
     <sequence> 
      <element name="Day"> 
       <simpleType> 
        <restriction base="string"> 
         <enumeration value="Monday"></enumeration> 
         <enumeration value="Tuesday"></enumeration> 
         <enumeration value="Wednesday"></enumeration> 
         <enumeration value="Thursday"></enumeration> 
         <enumeration value="Friday"></enumeration> 
        </restriction> 
       </simpleType> 
      </element> 
     </sequence> 
    </complexType> 

    <element name="DeliveryOrder" type="tns:DeliveryOrder"></element> 

    <complexType name="ItemsToBeDelivered"> 
     <sequence> 
      <element name="DaysForDelivery" type="tns:DaysForDelivery" maxOccurs="unbounded" minOccurs="1"> 
      </element> 
      <element name="Item" type="tns:Item" maxOccurs="unbounded" minOccurs="1"></element> 
     </sequence> 
    </complexType> 

    <complexType name="Item"> 
     <sequence> 
      <element name="Description"> 
       <simpleType> 
        <restriction base="string"> 
         <enumeration value="Regular Milk - 1 litre carton."> 
         </enumeration> 
         <enumeration value="Low Fat Milk - 1 litre carton."> 
         </enumeration> 
         <enumeration value="Strawberry Yoghurt - 100ml pot"> 
         </enumeration> 
         <enumeration value="Raspberry Yoghurt - 100ml pot"> 
         </enumeration> 
         <enumeration value="Cream - 125ml carton"></enumeration> 
         <enumeration value="Cream - 250ml carton"></enumeration> 
        </restriction> 
       </simpleType> 
      </element> 
      <element name="Quantity"> 
       <simpleType> 
        <restriction base="int"> 
         <minInclusive value="1"></minInclusive> 
        </restriction> 
       </simpleType> 
      </element> 
     </sequence> 
    </complexType> 
</schema> 

enter image description here

Exemple valide XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<DeliveryOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Depot> 
     <CountyName>Dublin</CountyName> 
    </Depot> 
    <CustomerDetails> 
     <FirstName>Firstname</FirstName> 
     <Surname>Surname</Surname> 
     <AddressLine1>AddressLine11</AddressLine1> 
     <AddressLine2>AddressLine21</AddressLine2> 
     <AddressLine3>AddressLine31</AddressLine3> 
     <County> 
      <CountyName>Dublin</CountyName> 
     </County> 
    </CustomerDetails> 
    <ItemsToBeDelivered> 
     <DaysForDelivery> 
      <Day>Monday</Day> 
     </DaysForDelivery> 
     <DaysForDelivery> 
      <Day>Tuesday</Day> 
     </DaysForDelivery> 
     <Item> 
      <Description>Regular Milk - 1 litre carton.</Description> 
      <Quantity>1</Quantity> 
     </Item> 
     <Item> 
      <Description>Regular Milk - 1 litre carton.</Description> 
      <Quantity>1</Quantity> 
     </Item> 
    </ItemsToBeDelivered> 
</DeliveryOrder> 

Si vous changez Tuesday à Monday vous obtenez:

Error occurred while loading [], line 22 position 17 
There is a duplicate key sequence 'Monday' for the 'http://tempuri.org/XMLSchema.xsd:UQ' key or unique identity constraint. 
Document1.xml is invalid. 
+0

Merci pour votre réponse, mais je n'ai pas d'erreur quand je saisis le même jour. Peut-être est nécessaire de donner mon xsd entier. (Donc, je vais mettre à jour ma question avec l'ensemble du code xsd) – kostas89

+0

@ kostas89, il ya une chose trompeuse ici: au lieu d'avoir la répétition de DaysForDelivery, vous devriez répéter Day à la place. Je pense que c'est ce qui a pu causer une certaine confusion ici ... –

+0

Cela fonctionne parfaitement, merci beaucoup! – kostas89

Questions connexes