sotn
sotn

Reputation: 2101

XML schema cardinality

Given the following xml data model, how would I specify in the xsd that the dept data model can have multiple employee nodes, but atleast one of them must have the <isSupervisor></isSupervisor> element. So, in other words, the <isSupervisor></isSupervisor> is not required for all employees but at least one employee should have it.

<Dept>
  <Employee>
    <name></name>
    <title></title>
    <isSupervisor></isSupervisor>
  </Employee>
  <Employee>
    <name></name>
    <title></title>
    <isSupervisor></isSupervisor>
  </Employee>
  <deptname></deptname>
  <deptid></deptid>
<Dept>

Upvotes: 2

Views: 2319

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

You need XSD 1.1 to declare assertions based on type content.

In the declaration for Employee, isSupervisor should be declared as optional:

<xs:element name="Employee">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="isSupervisor" type="xs:boolean" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

In the declaration for Dept, the xs:assert expression should always be true:

<xs:element name="Dept">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Employee" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element name="deptname"/>
            <xs:element name="deptid"/>
        </xs:sequence>
        <!-- true if at least one Employee/isSupervisor exists -->
        <xs:assert test="Employee/isSupervisor"/> 
    </xs:complexType>
</xs:element>

Upvotes: 1

Related Questions