RE: Can I constrain a sequence of elements to be unique?

Under the Style element declaration, add

<xsd:unique name="u">
 <xsd:selector xpath="SizeCategory"/>
 <xsd:field xpath="@Name"/>
</xsd:unique>

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: xmlschema-dev-request@w3.org 
> [mailto:xmlschema-dev-request@w3.org] On Behalf Of Paul Morgan
> Sent: 01 December 2004 22:45
> To: xmlschema-dev@w3.org
> Subject: Can I constrain a sequence of elements to be unique?
> 
> 
> Trying to create a schema that will allow a list of four 
> elements under a
> parent element.  At least one of the elements must occur.  
> Each of the four
> elements should be unique either as different element 
> definitions or with an
> element definition with an enumerated attribute.  This schema frament:
> 
> <xsd:element name="Style">
>  <xsd:complexType>
>   <xsd:sequence>
>    <xsd:element name="SizeCategory" minOccurs="1" maxOccurs="4" >
>     <xsd:complexType>
>      <xsd:attribute name="Name" use="required">
>       <xsd:simpleType>
>        <xsd:restriction base="xsd:string">
>         <xsd:enumeration value="Missy" />
>         <xsd:enumeration value="Petite" />
>         <xsd:enumeration value="Tall" />
>         <xsd:enumeration value="Womens" />
>        </xsd:restriction>
>       </xsd:simpleType>
>      </xsd:attribute>
>     </xsd:complexType>
>    </xsd:element>
>   </xsd:sequence>
>   <xsd:attribute name="Number" type="xsd:string" use="required" />
>  </xsd:complexType>
> </xsd:element>
> 
> allows for this XML:
> 
> <Style Number="51-2301">
>  <SizeCategory Name="Tall" />
>  <SizeCategory Name="Petite" />
>  <SizeCategory Name="Missy" />
>  <SizeCategory Name="Womens" />
> </Style>
> 
> but I want to modify that schema so that it does not allow this XML:
> 
> <Style Number="51-2301">
>  <SizeCategory Name="Missy" />
>  <SizeCategory Name="Missy" />
> </Style>
> 
> Is there some sort of constraint that can be placed on the 
> size categories
> to make the Name attribute unique under Style?
> 
> As an alternative I tried a different approach with this 
> schema fragment:
> 
> <xsd:element name="Style">
>   <xsd:complexType>
>    <xsd:all>
>     <xsd:element name="MissyCategory" type="sizeCategory" 
> minOccurs="0" />
>     <xsd:element name="PetiteCategory" type="sizeCategory" 
> minOccurs="0" />
>     <xsd:element name="TallCategory" type="sizeCategory" 
> minOccurs="0" />
>     <xsd:element name="WomensCategory" type="sizeCategory" 
> minOccurs="0" />
>     </xsd:all>
>    <xsd:attribute name="Number" type="xsd:string" use="required" />
>   </xsd:complexType>
>  </xsd:element>
> 
> <xsd:complexType name="sizeCategory" />
> 
> which allows for this XML:
> 
> <Style Number="51-2301">
>  <TallCategory />
>  <PetiteCategory />
>  <WomensCategory />
>  </Style>
> 
> but shouldn't allow this XML:
> 
> <Style Number="51-2301">
> </Style>
> 
> On this version of the Schema if  I add minOccurs="1" on the 
> all element
> would that force at least one sizeCategory element to occur?
> 
> Thanks,
> Paul
> 
> -- 
> Paul Morgan
> Senior Programmer Analyst - Retail
> J. Jill Group
> 100 Birch Pond Drive, PO Box 2009
> Tilton, NH 03276-2009
> Phone: (603) 266-2117
> Fax:   (603) 266-2333
> 
> 
> 
> 
> 
> 

Received on Thursday, 2 December 2004 09:08:08 UTC