<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="school" type="School">
 		<xs:key name="kRoom">
			<xs:selector xpath="rooms/room"/>
			<xs:field xpath="number"/>
		</xs:key>

		<xs:keyref name="krMeetingRoom" refer="kRoom">
			<xs:selector xpath="lectures/lecture/meetings/meeting"/>
			<xs:field xpath="room"/>
		</xs:keyref>

 		<xs:key name="kTeacher">
			<xs:selector xpath="teachers/teacher"/>
			<xs:field xpath="@teacher-id"/>
		</xs:key>

		<xs:keyref name="krTeacher" refer="kTeacher">
			<xs:selector xpath="lectures/lecture/teacher"/>
			<xs:field xpath="@teacher-id"/>
		</xs:keyref>

 		<xs:key name="kStudent">
			<xs:selector xpath="students/student"/>
			<xs:field xpath="@student-id"/>
		</xs:key>

		<xs:keyref name="krStudent" refer="kStudent">
			<xs:selector xpath="lectures/lecture/registered-students/student"/>
			<xs:field xpath="@student-id"/>
		</xs:keyref>
	</xs:element>
	
	<xs:element name="lecture" type="Lecture" />

	<xs:element name="student" type="Student" />

	<xs:element name="teacher" type="Teacher" />
	
	<xs:element name="rooms">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="room" type="Room" minOccurs="0" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	

	<xs:complexType name="School">
		<xs:sequence>
			<xs:element name="teachers">
				<xs:complexType>
					<xs:sequence>
						<xs:element ref="teacher" minOccurs="0" maxOccurs="unbounded"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="students">
				<xs:complexType>
					<xs:sequence>
						<xs:element ref="student" minOccurs="0" maxOccurs="unbounded"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="lectures">
				<xs:complexType>
					<xs:sequence>
						<xs:element ref="lecture" minOccurs="0" maxOccurs="unbounded"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="rooms">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="room" type="Room" minOccurs="0" maxOccurs="unbounded"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="Lecture">
		<xs:sequence>
			<xs:element name="name" type="xs:string" />
			<xs:element name="description" type="xs:string" />
			<xs:element name="program" type="LectureProgram" />
			<xs:element name="teacher">
				<xs:complexType>
					<xs:attribute name="teacher-id" type="xs:token" use="required"/>
				</xs:complexType>
			</xs:element>
			<xs:element name="registered-students">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="student" minOccurs="0" maxOccurs="unbounded">
							<xs:complexType>
								<xs:attribute name="student-id" type="xs:token" use="required"/>
							</xs:complexType>
						</xs:element>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="meetings">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="meeting" type="Meeting" minOccurs="0" maxOccurs="unbounded"/>
					</xs:sequence>
				</xs:complexType>
				<xs:unique name="kUniqueTerms">
					<xs:selector xpath="meeting" />
					<xs:field xpath="dayOfWeek" />
					<xs:field xpath="startTime" />
				</xs:unique>
			</xs:element>
		</xs:sequence>
		<xs:attribute name="usos-code" type="xs:token" />
	</xs:complexType>

	<xs:complexType name="Person">
		<xs:sequence>
			<xs:element name="first-name" type="xs:string" maxOccurs="3" />
			<xs:element name="last-name" type="xs:string" />
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="Student">
		<xs:complexContent>
			<xs:extension base="Person">
				<xs:sequence>
					<xs:element name="birth-date" type="xs:date" />
					<xs:element name="identification" type="Identification">
					</xs:element>
				</xs:sequence>
				<xs:attribute name="student-id" type="xs:token" use="required"/>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>

	<xs:complexType name="Teacher">
		<xs:complexContent>
			<xs:extension base="Person">
				<xs:attribute name="teacher-id" type="xs:token" use="required"/>
				<xs:attribute name="degree" type="Degree" use="required"/>
				<xs:attribute name="guest" type="xs:boolean" use="optional" default="false" />
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>

	<xs:complexType name="Identification">
		<xs:choice>
			<xs:element name="PESEL" type="xs:string" />
			<xs:sequence>
				<xs:element name="passport-nr" type="xs:string" />
				<xs:element name="country" type="xs:string" />
			</xs:sequence>
		</xs:choice>
	</xs:complexType>

	<xs:complexType name="LectureProgram">
		<xs:sequence>
			<xs:element name="item" type="xs:string" maxOccurs="unbounded" />
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="Room">
		<xs:sequence>
			<xs:element name="number" type="xs:token"/>
			<xs:element name="floor" type="Floor"/>
			<xs:element name="equipment" type="RoomEquipment"/>
			<xs:element name="seats" type="xs:unsignedShort" />
		</xs:sequence>
	</xs:complexType>
	
	<xs:complexType name="RoomEquipment">
		<xs:sequence/>
		<xs:attribute name="projector" type="xs:boolean" default="false"/>
		<xs:attribute name="computers" type="xs:boolean" default="false"/>
	</xs:complexType>
	
	<xs:complexType name="Meeting">
		<xs:sequence>
			<xs:element name="dayOfWeek" type="WeekDay"/>
			<xs:element name="startTime" type="xs:time"/>
			<xs:element name="endTime" type="xs:time"/>
			<xs:element name="room" type="xs:token"/>
		</xs:sequence>
	</xs:complexType>

	<xs:simpleType name="Degree">
		<xs:restriction base="xs:string">
			<xs:enumeration value="MSc" />
			<xs:enumeration value="PhD" />
			<xs:enumeration value="Prof" />
		</xs:restriction>
	</xs:simpleType>

	<xs:simpleType name="WeekDay">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Mon" />
			<xs:enumeration value="Tue" />
			<xs:enumeration value="Wed" />
			<xs:enumeration value="Thu" />
			<xs:enumeration value="Fri" />
			<xs:enumeration value="Sat" />
			<xs:enumeration value="Sun" />
		</xs:restriction>
	</xs:simpleType>

	<xs:simpleType name="Floor">
		<xs:restriction base="xs:byte">
			<xs:minInclusive value="0"/>
			<xs:maxInclusive value="4"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>
