<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:hl7-org:elm:r1" xmlns:sch="http://purl.oclc.org/dsdl/schematron" targetNamespace="urn:hl7-org:elm:r1" elementFormDefault="qualified">
	<xs:annotation>
		<xs:documentation> This file defines the root library type and element. </xs:documentation>
	</xs:annotation>
	<xs:include schemaLocation="clinicalexpression.xsd"/>
	<xs:complexType name="VersionedIdentifier">
		<xs:annotation>
			<xs:documentation>VersionedIdentifier is composed of three parts: (1) an optional system, or
        namespace, which provides a globally unique, stable scope for the identifier,
        (2) an identifier which identifies the set of all versions of a given resource, and
        (3) the actual version of the instance of interest in this set. The VersionedIdentifier
        therefore points to an individual 'versioned' instance of a resource such as the third
        version of a library.
      </xs:documentation>
		</xs:annotation>
		<xs:attribute name="id" type="xs:string"/>
		<xs:attribute name="system" type="xs:string" use="optional"/>
		<xs:attribute name="version" type="xs:string" use="optional"/>
	</xs:complexType>
	<xs:complexType name="DirectiveDef">
		<xs:attribute name="identifier" type="xs:string" use="required"/>
		<xs:attribute name="value" type="xs:string" use="optional"/>
	</xs:complexType>
	<xs:complexType name="UsingDef">
		<xs:annotation>
			<xs:documentation>Defines a data model that is available within the artifact.
      </xs:documentation>
		</xs:annotation>
		<xs:complexContent>
			<xs:extension base="Element">
				<xs:attribute name="localIdentifier" type="xs:string" use="required"/>
				<xs:attribute name="uri" type="xs:anyURI" use="required">
					<xs:annotation>
						<xs:documentation>The URI of the model that is being referenced. This URL must also be
              defined as a namespace in the root element of the document to allow for elements of
              the model to be referenced within the artifact.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="name" type="xs:string" use="optional">
				    <xs:annotation>
					    <xs:documentation>The fully qualified name of the model. If this is not provided, the localIdentifier attribute is used as the name of the model. The model qualifier is the namespace name of the model.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="version" type="xs:string" use="optional"/>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="IncludeDef">
		<xs:annotation>
			<xs:documentation>Includes a library for use within the artifact.</xs:documentation>
		</xs:annotation>
		<xs:complexContent>
			<xs:extension base="Element">
			    <xs:sequence>
					<xs:element name="bind" type="Tuple">
						<xs:annotation>
							<xs:documentation>Specifies values for parameters in the included library as expressions that may reference parameters in the current library. Bind expressions are evaluated in the same context as any parameter value expression, in that they only have access to parameter definitions in the library and may not contain any retrieve expressions. In addition, a bind clause on an included library overrides any parameter values provided by the evaluation environment.</xs:documentation>
						</xs:annotation>
					</xs:element>
				</xs:sequence>
				<xs:attribute name="localIdentifier" type="xs:string" use="required">
					<xs:annotation>
						<xs:documentation>A unique name within this artifact for the library reference. This
              name is used within this artifact to reference components of this
              library.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="mediaType" type="xs:anyURI" use="optional" default="application/elm+xml">
					<xs:annotation>
						<xs:documentation>Defines the type of the library. If this attribute is omitted, the
              library is assumed to be an ELM library artifact.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="path" type="xs:anyURI" use="required">
					<xs:annotation>
						<xs:documentation>Defines the path to the library.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="version" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>Optionally defines the required version number of the referenced
              library.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ContextDef">
		<xs:annotation>
			<xs:documentation>The ContextDef type defines a context definition statement. Note that this is a placeholder for the context statement within the library. The effect of the context definition is applied by the translator to the definitions that follow.</xs:documentation>
		</xs:annotation>
		<xs:complexContent>
			<xs:extension base="Element">
				<xs:attribute name="name" type="xs:string"/>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="InfoDef">
	    <xs:complexContent>
		    <xs:extension base="Element"/>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ContextInfoDef">
		<xs:annotation>
			<xs:documentation>Defines an available context type for the model.</xs:documentation>
		</xs:annotation>
		<xs:complexContent>
		    <xs:extension base="InfoDef">
				<xs:sequence>
					<xs:element name="contextType" type="NamedTypeSpecifier" minOccurs="1" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>Specifies the type for the context.</xs:documentation>
						</xs:annotation>
					</xs:element>
					<xs:element name="keyElement" type="xs:string" minOccurs="0" maxOccurs="unbounded">
						<xs:annotation>
							<xs:documentation>Specifies the key elements, in order, of the context type that form the reference key for the context. The elements taken together must form a unique identifier for instances of the context.</xs:documentation>
						</xs:annotation>
					</xs:element>
				</xs:sequence>
				<xs:attribute name="name" type="xs:string" use="required">
					<xs:annotation>
						<xs:documentation>Specifies the name of the context. This is the name that will be referenced by context statements within CQL.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="RelationshipInfoDef">
		<xs:annotation>
			<xs:documentation>Defines the relationship of a class to the context.</xs:documentation>
		</xs:annotation>
		<xs:complexContent>
		    <xs:extension base="InfoDef">
			    <xs:element name="relatedKeyElement" type="xs:string" minOccurs="0" maxOccurs="unbounded">
					<xs:annotation>
						<xs:documentation>Specifies the related key elements, in order, of the type that contain the reference to the context. There must be the same number of elements, and in the same order, as the target context.</xs:documentation>
					</xs:annotation>
				</xs:element>
				<xs:attribute name="context" type="xs:string" use="required">
					<xs:annotation>
						<xs:documentation>Specifies the target context of the relationship.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ClassInfoDefElement">
	    <xs:complexContent>
		    <xs:extension base="InfoDef">
				<xs:sequence>
					<xs:element name="elementTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
				<xs:attribute name="name" type="xs:string" use="required"/>
				<xs:attribute name="prohibited" type="xs:boolean" use="optional"/>
				<xs:attribute name="oneBased" type="xs:boolean" use="optional">
					<xs:annotation>
						<xs:documentation>Indicates that the starting index for a list-valued element is one. By default, lists are zero-based.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ClassInfoDef">
		<xs:annotation>
			<xs:documentation>The ClassInfoDef type models information associated with a single class in the data model.</xs:documentation>
		</xs:annotation>
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:sequence>
					<xs:element name="element" type="ClassInfoElementDef" minOccurs="0" maxOccurs="unbounded"/>
					<xs:element name="contextRelationship" type="RelationshipInfoDef" minOccurs="0" maxOccurs="unbounded"/>
					<xs:element name="targetContextRelationship" type="RelationshipInfoDef" minOccurs="0" maxOccurs="unbounded"/>
				</xs:sequence>
				<xs:attribute name="namespace" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>Namespace is the namespace of the class. This is typically just the name of the model, but can contain additional qualifiers.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="name" type="xs:string" use="required">
					<xs:annotation>
						<xs:documentation>The name attribute specifies the name of the type within the data model. This corresponds to the name of the class within the class model, or the name of the type in the case of an xsd. In FHIR, this corresponds to the name of the resource.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="identifier" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>The identifier attribute specifies a unique name for the class that may be independent of the name. In FHIR, this corresponds to the profile identifier.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="label" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>The label attribute specifies the name of the class as it is referenced from CQL. This is a user-friendly identifier for the class that can be used within CQL as an alternative way to reference the class.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="retrievable" type="xs:boolean" use="optional" default="false">
					<xs:annotation>
						<xs:documentation>The retrievable attribute specifies whether the class can be used within a retrieve statement. This flag is intended to allow the implementation to control whether or not the class can be used as a data access point for the data model.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="primaryCodePath" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>The primaryCodePath attribute specifies the path relative to the class that should be used to perform code filtering when a retrieve does not specify a code path.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="primaryValueSetPath" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>The primaryValueSetPath attribute specifies the path relative to the class that should be used to perform alternative value set matching when source data does not have a code defined.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="TypeInfoDef" abstract="true">
	    <xs:complexContent>
		    <xs:extension>
				<xs:sequence>
					<xs:element name="baseTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="SimpleTypeInfoDef">
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:attribute name="namespace" type="xs:string" use="optional">
					<xs:annotation>
						<xs:documentation>Namespace is the namespace of the type. This is typically just the name of the model, but can contain additional qualifiers.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
				<xs:attribute name="name" type="xs:string" use="required">
					<xs:annotation>
						<xs:documentation>Name is the unqualified name of the type within this model.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="IntervalTypeInfoDef">
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:sequence>
					<xs:element name="pointTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ListTypeInfoDef">
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:sequence>
					<xs:element name="elementTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="TupleTypeInfoDefElement">
	    <xs:complexContent>
		    <xs:extension base="InfoDef">
				<xs:sequence>
					<xs:element name="elementTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
				<xs:attribute name="name" type="xs:string" use="required"/>
				<xs:attribute name="prohibited" type="xs:boolean" use="optional"/>
				<xs:attribute name="oneBased" type="xs:boolean" use="optional">
					<xs:annotation>
						<xs:documentation>Indicates that the starting index for a list-valued element is one. By default, lists are zero-based.</xs:documentation>
					</xs:annotation>
				</xs:attribute>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="TupleTypeInfoDef">
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:sequence>
					<xs:element name="element" type="TupleTypeInfoDefElement" minOccurs="0" maxOccurs="unbounded"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ProfileInfoDef">
		<xs:complexContent>
			<xs:extension base="ClassInfoDef"/>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ConversionInfoDef">
	    <xs:complexContent>
		    <xs:extension base="InfoDef">
				<xs:sequence>
					<xs:element name="fromTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
					<xs:element name="toTypeSpecifier" type="TypeSpecifier" minOccurs="1" maxOccurs="1"/>
				</xs:sequence>
				<xs:attribute name="functionName" type="xs:string" use="required"/>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="ChoiceTypeInfoDef">
		<xs:complexContent>
			<xs:extension base="TypeInfoDef">
				<xs:sequence>
					<xs:element name="choice" type="TypeSpecifier" minOccurs="0" maxOccurs="unbounded"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:element name="library" type="Library">
		<xs:annotation>
			<xs:documentation>library represents a serialized library of CQL-Epression Logic
        Model.</xs:documentation>
		</xs:annotation>
	</xs:element>
	<xs:complexType name="Library">
		<xs:annotation>
			<xs:documentation>A Library is an instance of a CQL-ELM library. </xs:documentation>
		</xs:annotation>
		<xs:complexContent>
			<xs:extension base="Element">
				<xs:sequence>
					<xs:element name="identifier" type="VersionedIdentifier">
						<xs:annotation>
							<xs:documentation>The identifier element defines a unique identifier for this library,
								and optionally, a system (or namespace) and version. </xs:documentation>
						</xs:annotation>
					</xs:element>
					<xs:element name="schemaIdentifier" type="VersionedIdentifier">
						<xs:annotation>
							<xs:documentation>This is the identifier of the XML schema (and its version) which
                governs the structure of this Library.</xs:documentation>
						</xs:annotation>
					</xs:element>
					<xs:element name="directives" minOccurs="0">
						<xs:annotation>
							<xs:documentation>Set of directives defined in the library</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="DirectiveDef" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="usings" minOccurs="0">
						<xs:annotation>
							<xs:documentation>Set of data models referenced in the Expression objects in this
                knowledge artifact. </xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="UsingDef" maxOccurs="unbounded">
									<xs:annotation>
										<xs:documentation>A reference to a data model that is used in the artifact,
                      e..g., the Virtual Medical Record. </xs:documentation>
									</xs:annotation>
								</xs:element>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="includes" minOccurs="0">
						<xs:annotation>
							<xs:documentation>Set of libraries referenced by this artifact. Components of
                referenced libraries may be used within this artifact.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="IncludeDef" maxOccurs="unbounded">
									<xs:annotation>
										<xs:documentation>A reference to a library whose components can be used within
                      the artifact. </xs:documentation>
									</xs:annotation>
								</xs:element>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="parameters" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The parameters defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="ParameterDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="codeSystems" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The code systems defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="CodeSystemDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="valueSets" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The value sets defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="ValueSetDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="codes" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The codes defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="CodeDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="concepts" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The concepts defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="ConceptDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="contexts" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The contexts defined within this library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="ContextDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="statements" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The statements section contains the expression and function definitions for the library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="ExpressionDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
					<xs:element name="infoDefs" minOccurs="0" maxOccurs="1">
						<xs:annotation>
							<xs:documentation>The infoDefs section contains the model definitions for the library.</xs:documentation>
						</xs:annotation>
						<xs:complexType>
							<xs:sequence>
								<xs:element name="def" type="InfoDef" minOccurs="0" maxOccurs="unbounded"/>
							</xs:sequence>
						</xs:complexType>
					</xs:element>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
</xs:schema>
