简体   繁体   中英

How to limit number of unordered XML elements in XSD?

I have XML that looks like this:

  <Root id="1a">
      <Name>TOM</Name>
      <StudentID>123</StudentID>
      <Description>Detailed description of the student</Description>
      <Subjects>
          <subject1>Chemistry</subject1>
          <subject2>Physics</subject2>
          <subject3>Maths</subject3>
          <subject4>History</subject4>
          <subject1>ChemistryLab</subject1>
      </Subjects>
  </Root>

Constraints : Subjects element must have at least one subject and at maximum five subjects in any combination of following elements: subject1, subject2, subject3, subject4 (ie if all five outputs will be subject1, it's still valid version of Subjects element, it also does not matter in which order these elements will appear).

Attempted XSD

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="SubjectsType">
        <xs:sequence>
            <xs:element name="subject1" type="xs:string" minOccurs="0" maxOccurs="5"/>
            <xs:element name="subject2" type="xs:string" minOccurs="0" maxOccurs="5"/>
            <xs:element name="subject3" type="xs:string" minOccurs="0" maxOccurs="5"/>
            <xs:element name="subject4" type="xs:string" minOccurs="0" maxOccurs="5"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="RootType">
        <xs:sequence>
            <xs:element name="Name" type="xs:string"/>
            <xs:element name="StudentID" type="xs:string"/>
            <xs:element name="Description" type="xs:string"/>
            <xs:element name="Subjects" type="SubjectsType"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:integer" use="required"/>
    </xs:complexType>

    <xs:element name="Root" type="RootType"/> 
</xs:schema>

The key to achieving your goal is to use xs:choice with minOccurs="1" and maxOccurs="5" :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="StudentID" type="xs:integer"/>
        <xs:element name="Description" type="xs:string"/>
        <xs:element name="Subjects">
          <xs:complexType>
            <xs:choice  minOccurs="1" maxOccurs="5">
              <xs:element name="subject1" type="xs:string"/>
              <xs:element name="subject2" type="xs:string"/>
              <xs:element name="subject3" type="xs:string"/>
              <xs:element name="subject4" type="xs:string"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

This will allow between 1 and 5 of child subject(n in 1..4) elements in any order, including duplicates, as requested.

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