简体   繁体   中英

Define key in XMS element

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM