<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
>	
	<!-- The root element of every structured data definition -->
	<xs:element name="system-data-structure">
		<xs:complexType>
			<xs:choice minOccurs="1" maxOccurs="unbounded">
				<xs:element ref="group" />
				<xs:element ref="text" />
				<xs:element ref="asset" />
			</xs:choice>
		</xs:complexType>
	</xs:element>

	<!-- 'group' node type allows for nesting and grouping of text and asset elements -->
	<xs:element name="group">
		<xs:complexType>
			<xs:choice maxOccurs="unbounded" minOccurs="1">
				<xs:element ref="text"/>
				<xs:element ref="asset"/>
				<xs:element ref="group"/>
			</xs:choice>
			<xs:attributeGroup ref="standardAttributes"/>
		</xs:complexType>
	</xs:element>

	<!-- 
		'text' node type is used for creating all textual data structures from plain text boxes
		to WYSWIYGs, to radios, dropdowns, and checkboxes
	-->
	<xs:element name="text">
		<xs:complexType>
			<!-- must only have one type of item depending on the node's 'type' attribute -->
			<xs:choice minOccurs="0" maxOccurs="1">
				<xs:choice minOccurs="1" maxOccurs="unbounded">
					<xs:element name="checkbox-item">
						<xs:complexType>
							<xs:attribute name="value" use="required" type="cappedString"/>
							<xs:attribute name="checked" type="xs:boolean"/>
						</xs:complexType>
					</xs:element>
				</xs:choice>
				<xs:choice minOccurs="1" maxOccurs="unbounded">
					<xs:element name="dropdown-item">
						<xs:complexType>
							<xs:attribute name="value" use="required" type="cappedString"/>
						</xs:complexType>
					</xs:element>				
				</xs:choice>
				<xs:choice minOccurs="1" maxOccurs="unbounded">
					<xs:element name="radio-item">
						<xs:complexType>
							<xs:attribute name="value" use="required" type="cappedString"/>
						</xs:complexType>
					</xs:element>
				</xs:choice>
				<xs:choice minOccurs="1" maxOccurs="unbounded">
					<xs:element name="selector-item">
						<xs:complexType>
							<xs:attribute name="value" use="required" type="cappedString"/>
							<xs:attribute name="selected" type="xs:boolean"/>
						</xs:complexType>
					</xs:element>
				</xs:choice>
			</xs:choice>
			
			<!-- Common attributes -->
			<xs:attributeGroup ref="standardAttributes" />
			<xs:attribute name="required" type="xs:boolean" />
			<xs:attribute name="help-text" type="xs:string"/>
			
			<!-- 'text' node specific attributes --> 
			<xs:attribute name="default" type="cappedString"/>
			<xs:attribute name="type" type="textType"/>
			<xs:attribute name="multi-line" type="xs:boolean"/>
			<xs:attribute name="wysiwyg" type="xs:boolean"/>
			<xs:attribute name="wysiwyg-toolbar-remove" type="xs:string"/>
			<xs:attribute name="rows" type="xs:nonNegativeInteger"/>
			<xs:attribute name="cols" type="xs:nonNegativeInteger"/>
			<xs:attribute name="size" type="xs:nonNegativeInteger"/>
			<xs:attribute name="maxlength" type="xs:nonNegativeInteger"/>
			<xs:attribute name="regular-expression" type="xs:string"/>
			<xs:attribute name="input-data-format" type="xs:string"/>
		</xs:complexType>
	</xs:element>
	
	<!-- 'asset' node type for selecting a system asset -->
	<xs:element name="asset">
		<xs:complexType>
			<!-- Common attributes -->
			<xs:attributeGroup ref="standardAttributes"/>
			<xs:attribute name="required" type="xs:boolean" />
			<xs:attribute name="help-text" type="cappedString"/>
			
			<xs:attribute name="type" use="required">
				<!-- Asset type enumeration -->
				<xs:simpleType>
					<xs:restriction base="xs:NMTOKEN">
						<xs:enumeration value="page" />
						<xs:enumeration value="block" />
						<xs:enumeration value="file" />
						<xs:enumeration value="symlink" />
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
			
			<!-- 
				Render content depth can be any non-negative integer or 'unlimited'
				to render to any depth
			-->
			<xs:attribute name="render-content-depth">
				<xs:simpleType>
					<xs:union>
						<xs:simpleType>
							<xs:restriction base="xs:nonNegativeInteger"/>
						</xs:simpleType>
						<xs:simpleType>
							<xs:restriction base="xs:NMTOKEN">
								<xs:enumeration value="unlimited"/>
							</xs:restriction>
						</xs:simpleType>
					</xs:union>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
	</xs:element>

	<!-- A String with length capped at 255 characters -->
	<xs:simpleType name="cappedString">
		<xs:restriction base="xs:string">
			<xs:maxLength value="255" />
			<xs:minLength value="1"/>
		</xs:restriction>
	</xs:simpleType>
	
	<!--  enumeration for 'text' node's attribute 'type' -->
	<xs:simpleType name="textType">
		<xs:restriction base="xs:NMTOKEN">
			<xs:enumeration value="radiobutton"/>
			<xs:enumeration value="multi-selector"/>
			<xs:enumeration value="checkbox" />
			<xs:enumeration value="dropdown" />
			<xs:enumeration value="datetime" />
			<!-- 'calendar' deprecated in favor of datetime -->
			<xs:enumeration value="calendar"/>
		</xs:restriction>
	</xs:simpleType>
	
	<!-- Set of attributes for all three node types (asset, group, text) -->
	<xs:attributeGroup name="standardAttributes">
		<!-- 
			Our identifier attribute must be a valid XML element name as it 
			becomes an element name in the rendered structured data 
		-->
		<xs:attribute name="identifier" type="xs:Name" use="required" />
		<xs:attribute name="label" type="cappedString" use="required" />
		<xs:attribute name="multiple" type="xs:boolean" />
		<xs:attribute name="maximum-number" type="xs:nonNegativeInteger" />
		<xs:attribute name="minimum-number" type="xs:nonNegativeInteger" />
		<xs:attribute name="restrict-to-groups" type="xs:string"/>
	</xs:attributeGroup>
</xs:schema>