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

	<xs:complexType name="empty" />

	<!-- restrictedLengthString -->
	<xs:simpleType name="restrictedLengthString">
		<xs:restriction base="xs:string">
			<xs:maxLength value="255" />
		</xs:restriction>
	</xs:simpleType>

	<!-- moderateRestrictedLengthString -->
	<xs:simpleType name="moderateRestrictedLengthString">
		<xs:restriction base="xs:string">
			<xs:maxLength value="2048" />
		</xs:restriction>
	</xs:simpleType>

	<!-- uuid -->
	<xs:simpleType name="uuid">
		<xs:restriction base="xs:string">
			<xs:pattern value="[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}"></xs:pattern>
		</xs:restriction>
	</xs:simpleType>

	<!-- LINK TAG -->
	<xs:attributeGroup name="link">
		<xs:attribute name="href" type="moderateRestrictedLengthString" />
	</xs:attributeGroup>

	<xs:complexType name="link" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContent" />
		</xs:choice>
		<xs:attributeGroup ref="link" />
	</xs:complexType>

	<xs:complexType name="linkWithInput" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContentWithInput" />
		</xs:choice>
		<xs:attributeGroup ref="link" />
	</xs:complexType>

	<!-- MEDIA TAGS -->
	<xs:complexType name="video">
		<xs:attribute name="source" type="restrictedLengthString" use="required" />
		<xs:attribute name="width" type="xs:unsignedInt" use="optional" />
		<xs:attribute name="height" type="xs:unsignedInt" use="optional" />
	</xs:complexType>

	<xs:complexType name="img">
		<xs:attribute name="source" type="restrictedLengthString" use="required" />
		<xs:attribute name="width" type="xs:unsignedInt" use="required" />
		<xs:attribute name="height" type="xs:unsignedInt" use="required" />
	</xs:complexType>

	<xs:complexType name="media">
		<xs:attribute name="source" type="restrictedLengthString" use="required" />
	</xs:complexType>

	<xs:complexType name="latexFormula">
		<xs:attribute name="width" type="xs:unsignedInt" use="required" />
		<xs:attribute name="height" type="xs:unsignedInt" use="required" />
		<xs:attribute name="source" type="restrictedLengthString" use="required" />
		<xs:attribute name="sourceFormula" type="moderateRestrictedLengthString" use="required" />
	</xs:complexType>
	
	<!-- ALIGNABLE -->
	<xs:attributeGroup name="alignmentAttributes">
		<xs:attribute name="align">
			<xs:simpleType>
				<xs:restriction base="xs:string">
					<xs:enumeration value="left" />
					<xs:enumeration value="center" />
					<xs:enumeration value="right" />
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="indent" type="xs:integer" />
	</xs:attributeGroup>

	<!-- INPUT WITHOUT ANSWER -->
	<xs:complexType name="input">
		<xs:attribute name="uuid" type="uuid" use="required" />
	</xs:complexType>

	<!-- FORMATTED TEXT CONTENT -->
	<xs:complexType name="formattedTextContent" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContent" />
		</xs:choice>
	</xs:complexType>

	<xs:complexType name="formattedTextContentWithInput" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContentWithInput" />
		</xs:choice>
	</xs:complexType>

	<xs:group name="formattedTextContent">
		<xs:choice>
			<xs:element name="sub" type="formattedTextContent" />
			<xs:element name="super" type="formattedTextContent" />
			<xs:element name="b" type="formattedTextContent" />
			<xs:element name="i" type="formattedTextContent" />
			<xs:element name="u" type="formattedTextContent" />
			<xs:element name="font" type="font" />
			<xs:element name="link" type="link" />
			<xs:element name="img" type="img" />
			<xs:element name="video" type="video" />
			<xs:element name="audio" type="media" />
			<xs:element name="latexformula" type="latexFormula" />
			<xs:element name="formula" type="moderateRestrictedLengthString" />
			<xs:element name="br" type="empty" />
		</xs:choice>
	</xs:group>

	<xs:group name="formattedTextContentWithInput">
		<xs:choice>
			<xs:element name="sub" type="formattedTextContentWithInput" />
			<xs:element name="super" type="formattedTextContentWithInput" />
			<xs:element name="b" type="formattedTextContentWithInput" />
			<xs:element name="i" type="formattedTextContentWithInput" />
			<xs:element name="u" type="formattedTextContentWithInput" />
			<xs:element name="font" type="fontWithInput" />
			<xs:element name="link" type="linkWithInput" />
			<xs:element name="img" type="img" />
			<xs:element name="video" type="video" />
			<xs:element name="audio" type="media" />
			<xs:element name="latexformula" type="latexFormula" />
			<xs:element name="formula" type="moderateRestrictedLengthString" />
			<xs:element name="br" type="empty" />
			<xs:element name="input" type="input" />
		</xs:choice>
	</xs:group>

	<!-- STRUCTURE ELEMENT TYPES -->
	<xs:complexType name="alignmentAttributesAndContent" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContent" />
		</xs:choice>
		<xs:attributeGroup ref="alignmentAttributes" />
	</xs:complexType>

	<xs:complexType name="alignmentAttributesAndContentAndInput" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContentWithInput" />
		</xs:choice>
		<xs:attributeGroup ref="alignmentAttributes" />
	</xs:complexType>

	<!-- FONT TAG -->
	<xs:attributeGroup name="font">
		<xs:attribute name="family" type="restrictedLengthString" />
		<xs:attribute name="color">
			<xs:simpleType>
				<xs:restriction base="xs:string">
					<xs:pattern value="#[0-9A-Fa-f]{6}" />
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="size" type="xs:unsignedInt" />
	</xs:attributeGroup>

	<xs:complexType name="font" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContent" />
		</xs:choice>
		<xs:attributeGroup ref="font" />
	</xs:complexType>

	<xs:complexType name="fontWithInput" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:group ref="formattedTextContentWithInput" />
		</xs:choice>
		<xs:attributeGroup ref="font" />
	</xs:complexType>

	<!-- LIST -->
	<xs:complexType name="list">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="li">
				<xs:complexType mixed="true">
					<xs:choice minOccurs="0" maxOccurs="unbounded">
						<xs:group ref="formattedTextContent" />
						<xs:element name="para" type="alignmentAttributesAndContent" />
						<xs:element name="ol" type="list" />
						<xs:element name="ul" type="list" />
						<xs:element name="table" type="table" />
					</xs:choice>
					<xs:attributeGroup ref="alignmentAttributes" />
				</xs:complexType>
			</xs:element>
		</xs:choice>
		<xs:attributeGroup ref="alignmentAttributes" />
	</xs:complexType>

	<xs:complexType name="listWithInput">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="li">
				<xs:complexType mixed="true">
					<xs:choice minOccurs="0" maxOccurs="unbounded">
						<xs:group ref="formattedTextContentWithInput" />
						<xs:element name="para" type="alignmentAttributesAndContentAndInput" />
						<xs:element name="ol" type="listWithInput" />
						<xs:element name="ul" type="listWithInput" />
						<xs:element name="table" type="tableWithInput" />
					</xs:choice>
					<xs:attributeGroup ref="alignmentAttributes" />
				</xs:complexType>
			</xs:element>
		</xs:choice>
		<xs:attributeGroup ref="alignmentAttributes" />
	</xs:complexType>

	<!-- TABLE -->
	<xs:complexType name="table">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="tr">
				<xs:complexType>
					<xs:choice minOccurs="0" maxOccurs="unbounded">
						<xs:element name="td">
							<xs:complexType mixed="true">
								<xs:choice minOccurs="0" maxOccurs="unbounded">
									<xs:group ref="formattedTextContent" />
									<xs:element name="para" type="alignmentAttributesAndContent" />
									<xs:element name="ol" type="list" />
									<xs:element name="ul" type="list" />
									<xs:element name="table" type="table" />
								</xs:choice>
								<xs:attributeGroup ref="alignmentAttributes" />
							</xs:complexType>
						</xs:element>
					</xs:choice>
				</xs:complexType>
			</xs:element>
		</xs:choice>
	</xs:complexType>

	<xs:complexType name="tableWithInput">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="tr">
				<xs:complexType>
					<xs:choice minOccurs="0" maxOccurs="unbounded">
						<xs:element name="td">
							<xs:complexType mixed="true">
								<xs:choice minOccurs="0" maxOccurs="unbounded">
									<xs:group ref="formattedTextContentWithInput" />
									<xs:element name="para" type="alignmentAttributesAndContentAndInput" />
									<xs:element name="ol" type="listWithInput" />
									<xs:element name="ul" type="listWithInput" />
									<xs:element name="table" type="tableWithInput" />
								</xs:choice>
								<xs:attributeGroup ref="alignmentAttributes" />
							</xs:complexType>
						</xs:element>
					</xs:choice>
				</xs:complexType>
			</xs:element>
		</xs:choice>
	</xs:complexType>

	<!-- STRUCTURE ELEMENTS -->
	<xs:complexType name="structureElements">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="para" type="alignmentAttributesAndContent" />
			<xs:element name="ol" type="list" />
			<xs:element name="ul" type="list" />
			<xs:element name="table" type="table" />
		</xs:choice>
	</xs:complexType>

	<xs:complexType name="structureElementsWithInput">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="para" type="alignmentAttributesAndContentAndInput" />
			<xs:element name="ol" type="listWithInput" />
			<xs:element name="ul" type="listWithInput" />
			<xs:element name="table" type="tableWithInput" />
		</xs:choice>
	</xs:complexType>

	<!-- CARD CONTENT FOR VOCABULARYCARDS -->
	<xs:complexType name="vocabularyContent">
		<xs:sequence>

			<!-- VOCAB IMAGE -->
			<xs:element minOccurs="0" maxOccurs="1" name="img" type="media" />

			<xs:sequence minOccurs="0">
				<!-- Paragraphen vor dem mit dem Vokabeltag -->
				<xs:element name="para" type="paraOnlyBr" minOccurs="0" maxOccurs="unbounded" />
				<!-- Paragraphen mit dem Vokabeltag -->
				<xs:element name="vocabPara">
					<xs:complexType mixed="true">
						<xs:sequence>
							<xs:element name="br" type="empty" minOccurs="0" maxOccurs="unbounded" />
							<xs:element name="vocabulary">
								<xs:complexType mixed="true">
									<xs:choice minOccurs="0" maxOccurs="unbounded">
										<xs:element name="br" type="empty" />
									</xs:choice>
								</xs:complexType>
							</xs:element>
							<xs:element name="br" type="empty" minOccurs="0" maxOccurs="unbounded" />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<!-- Paragraphen nach dem mit dem Vokabeltag -->
				<xs:element name="para" type="paraOnlyBr" minOccurs="0" maxOccurs="unbounded" />
			</xs:sequence>

			<!-- VOCAB AUDIO -->
			<xs:element minOccurs="0" maxOccurs="1" name="audio" type="media" />

		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="paraOnlyBr" mixed="true">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="br" type="empty"></xs:element>
		</xs:choice>
	</xs:complexType>

	<!-- CARD TYPES -->

	<!-- TEXT -->
	<xs:complexType name="textfilecard">
		<xs:sequence>
			<xs:element name="question" type="structureElements" />
			<xs:element name="answer" type="structureElements" />
			<xs:element name="crib" type="structureElements" minOccurs="0" />
		</xs:sequence>
		<xs:attribute name="userCardID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- VOCAB -->
	<xs:complexType name="vocabularycard">
		<xs:sequence>
			<xs:element name="vocabularyQuestion" type="vocabularyContent" />
			<xs:element name="vocabularyAnswer" type="vocabularyContent" />
			<xs:element name="crib" type="structureElements" minOccurs="0" />
		</xs:sequence>
		<xs:attribute name="userCardID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- MULTIPLE CHOICE -->
	<xs:complexType name="multiplechoicefilecard">
		<xs:sequence>
			<xs:element name="question" type="structureElements" />
			<xs:element name="mcAnswers">
				<xs:complexType>
					<xs:sequence minOccurs="0" maxOccurs="unbounded">
						<xs:element name="mcAnswer">
							<xs:complexType>
								<xs:sequence>
									<xs:element name="answer" type="structureElements" />
									<xs:element name="explanation" type="structureElements" minOccurs="0" />
								</xs:sequence>
								<xs:attribute name="correct" type="xs:boolean" use="required" />
							</xs:complexType>
						</xs:element>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="crib" type="structureElements" minOccurs="0" />
		</xs:sequence>
		<xs:attribute name="userCardID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- CLOZE -->
	<xs:complexType name="clozefilecard">
		<xs:sequence>
			<xs:element name="answer" type="structureElementsWithInput" />
			<xs:element name="inputAnswers">
				<xs:complexType>
					<xs:sequence minOccurs="0" maxOccurs="unbounded">
						<xs:element name="inputAnswer">
							<xs:complexType>
								<xs:attribute name="answer" type="restrictedLengthString" use="required" />
								<xs:attribute name="uuid" type="uuid" use="required" />
							</xs:complexType>
						</xs:element>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="crib" type="structureElements" minOccurs="0" />
		</xs:sequence>
		<xs:attribute name="pretendPossibleanswers" type="xs:boolean" use="required" />
		<xs:attribute name="casesensitive" type="xs:boolean" use="required" />
		<xs:attribute name="userCardID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- ROOT -->
	<xs:element name="BYXML">
		<xs:complexType>
			<xs:choice minOccurs="0" maxOccurs="unbounded">
				<xs:element name="lesson" type="lesson" />
				<xs:element name="category" type="category" />
				<xs:element name="mediaLesson" type="mediaLesson" />
			</xs:choice>
			<xs:attribute name="version" use="required">
				<xs:simpleType>
					<!-- Might be extended some time -->
					<xs:restriction base="xs:string">
						<xs:enumeration value="1.0" />
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
		<xs:unique name="unique-userLessonID">
			<xs:selector xpath=".//lesson | .//mediaLesson" />
			<xs:field xpath="@userLessonID" />
		</xs:unique>
		<xs:unique name="unique-userCategoryID">
			<xs:selector xpath=".//category" />
			<xs:field xpath="@userCategoryID" />
		</xs:unique>
		<xs:unique name="unique-userCardID">
			<xs:selector xpath=".//lesson/*" />
			<xs:field xpath="@userCardID" />
		</xs:unique>
	</xs:element>

	<!-- CATEGORY -->
	<xs:complexType name="category">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="lesson" type="lesson" />
			<xs:element name="mediaLesson" type="mediaLesson" />
		</xs:choice>
		<xs:attribute name="title" type="restrictedLengthString" use="required" />
		<xs:attribute name="userCategoryID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- LESSON -->
	<xs:complexType name="lesson">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="textfilecard" type="textfilecard" />
			<xs:element name="multiplechoicefilecard" type="multiplechoicefilecard" />
			<xs:element name="clozefilecard" type="clozefilecard">
				<xs:key name="answerid">
					<xs:selector xpath=".//inputAnswer" />
					<xs:field xpath="@uuid"></xs:field>
				</xs:key>
				<xs:keyref name="answeridRef" refer="answerid">
					<xs:selector xpath=".//input" />
					<xs:field xpath="@uuid" />
				</xs:keyref>
			</xs:element>
			<xs:element name="vocabularycard" type="vocabularycard" />
			<xs:element name="lesson" type="lesson" />
			<xs:element name="mediaLesson" type="mediaLesson" />
		</xs:choice>
		<xs:attribute name="title" type="restrictedLengthString" use="required" />
		<xs:attribute name="userLessonID" type="restrictedLengthString" />
	</xs:complexType>

	<!-- MEDIA LESSON -->
	<xs:complexType name="mediaLesson">
		<xs:attribute name="title" type="restrictedLengthString" use="required" />
		<xs:attribute name="originalMediaName" type="restrictedLengthString" use="required" />
		<xs:attribute name="userLessonID" type="restrictedLengthString" />
	</xs:complexType>


</xs:schema>