I have a piece of XMS like this:
<xs:element name="student" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:int" name="id"/>
</xs:sequence>
</xs:complexType>
</xs:element>
I want to define that the id element inside the student element has to be unique among all the students. Is there a way of doing so using xs:key or something like that?
You can make use of the xs:unique
element which makes sure that the by XPath specified (child) elements only occur once.
In your case, this could look like this:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:int" name="id"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="theID">
<xs:selector xpath="student/id"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
This assures that the values of all id
s of the student
element are unique.