<?xml version='1.0' encoding='UTF-8'?>
<xs:schema version="0.2.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:drmd="https://example.org/drmd"
           xmlns:dcc="https://ptb.de/dcc"
           xmlns:si="https://ptb.de/si"
           xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
           targetNamespace="https://example.org/drmd"
           elementFormDefault="qualified">

    <!-- Remote canonical dependencies (preferred for full validation) -->
    <!-- Local imports (offline validation); canonical remote URLs can be used alternatively -->
    <xs:import namespace="https://ptb.de/dcc"
               schemaLocation="../../imports/dcc.xsd"/>
    <xs:import namespace="https://ptb.de/si"
               schemaLocation="../../imports/SI_Format.xsd"/>
    <xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
               schemaLocation="../../imports/xmldsig-core-schema.xsd"/>

        <!-- ========================================================= -->
        <!--  Simple Types (global for reuse & validation)             -->
        <!-- ========================================================= -->

        <!-- Semantic version: major.minor.patch -->
        <xs:simpleType name="schemaVersionType">
            <xs:restriction base="xs:string">
                <xs:pattern value="\d+\.\d+\.\d+"/>
            </xs:restriction>
        </xs:simpleType>

        <!-- Title of the DRMD document (extracted from inline enum) -->
        <xs:simpleType name="titleOfDocumentType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="productInformationSheet"/>
                <xs:enumeration value="referenceMaterialCertificate"/>
            </xs:restriction>
        </xs:simpleType>

        <!-- identifier and list building blocks -->
    <xs:complexType name="identifierType">
      <xs:sequence>
        <xs:element name="scheme" type="dcc:notEmptyStringType"/>
        <xs:element name="value"  type="dcc:notEmptyStringType"/>
        <xs:element name="link"   type="xs:anyURI" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="id"      type="xs:ID"     use="optional"/>
      <xs:attribute name="refId"   type="xs:IDREFS" use="optional"/>
      <xs:attribute name="refType" type="drmd:refTypesType" use="optional"/>
    </xs:complexType>

    <xs:complexType name="organizationIdentifierListType">
      <xs:sequence>
        <xs:element name="organizationIdentifier"
                    type="drmd:identifierType"
                    maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="materialIdentifierListType">
      <xs:sequence>
        <xs:element name="materialIdentifier"
                    type="drmd:identifierType"
                    maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="propertyIdentifierListType">
      <xs:sequence>
        <xs:element name="propertyIdentifier"
                    type="drmd:identifierType"
                    maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="documentIdentifierListType">
      <xs:sequence>
        <xs:element name="documentIdentifier"
                    type="drmd:identifierType"
                    maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="refTypesType">
      <xs:restriction base="xs:string">
        <xs:enumeration value="basic_certificateIdentification"/>
        <xs:enumeration value="basic_measuredValue"/>
      </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="quantityType">
      <xs:complexContent>
        <xs:extension base="dcc:primitiveQuantityType">
          <xs:sequence>
            <xs:element name="propertyIdentifiers"
                        type="drmd:propertyIdentifierListType"
                        minOccurs="0"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>

    <xs:annotation>
        <xs:documentation>
            DRMD - Digital Reference Material Document
            Copyright (c) 2024 Bundesanstalt für Materialforschung und -prüfung (BAM)

            This XML Schema Definition (XSD) is free software: you can redistribute
            it and/or modify it under the terms of the GNU Lesser General Public License
            as published by the Free Software Foundation, version 3 of the License.
            This XSD is distributed in the hope that it will be useful, but WITHOUT ANY
            WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
            FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
            more details. The development of the Digital Reference Material Document (DRMD)
            is partially funded and supported by the QI-Digital project.
        </xs:documentation>
    </xs:annotation>

    <!-- Root element -->
    <xs:element name="digitalReferenceMaterialDocument"
                type="drmd:digitalReferenceMaterialDocumentType"/>

    <!-- Root type -->
    <xs:complexType name="digitalReferenceMaterialDocumentType">
        <xs:annotation>
            <xs:documentation>
                The root element that contains administrative data, materials,
                measurement results, and signatures of the DRMD.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <!-- administrativeData no longer holds <materials> -->
            <xs:element name="administrativeData"
                        type="drmd:administrativeDataType"/>
            <!-- moved to root -->
            <xs:element name="materials"
                        type="drmd:materialListType"/>
            <xs:element name="materialPropertiesList"
                        type="drmd:materialPropertiesListType"/>
            <xs:element name="statements"
                        type="drmd:statementListType"/>
            <xs:element name="comment"
                        type="xs:string" minOccurs="0"/>
            <xs:element name="document"
                        type="dcc:byteDataType" minOccurs="0"/>
            <xs:element ref="ds:Signature" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    <xs:attribute name="schemaVersion"
              type="drmd:schemaVersionType"
              use="required"/>
    </xs:complexType>

    <!-- administrativeData without materials -->
    <xs:complexType name="administrativeDataType">
        <xs:annotation>
            <xs:documentation>
                Contains core data, producer information, responsible persons,
                and statements related to the DRMD.
            </xs:documentation>
        </xs:annotation>
        <!-- changed from xs:all to xs:sequence for extensibility and deterministic order -->
        <xs:sequence>
            <xs:element name="coreData" type="drmd:coreDataType"/>
            <xs:element name="referenceMaterialProducer" type="drmd:referenceMaterialProducerType"/>
            <xs:element name="respPersons" type="dcc:respPersonListType"/>
        </xs:sequence>
    </xs:complexType>

	<xs:complexType name="coreDataType">
		<xs:annotation>
			<xs:documentation> Contains the core information about the document, such as title,
				unique identifier, and issue date. </xs:documentation>
		</xs:annotation>
        <xs:sequence>
            <xs:element name="titleOfTheDocument" type="drmd:titleOfDocumentType"/>
                        <xs:element name="uniqueIdentifier"
                                    type="dcc:notEmptyStringType"/>
                        <xs:element name="documentIdentifiers"
                                    type="drmd:documentIdentifierListType"
                                    minOccurs="0"/>
        	<xs:element name="validity" 
						type="drmd:ValidityType"
						 minOccurs="1" />
		</xs:sequence>
	</xs:complexType>

    <xs:complexType name="ValidityType">
        <xs:choice>
            <xs:element name="untilRevoked" type="xs:boolean" fixed="true" />
            <xs:element name="timeAfterDispatch">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="dispatchDate" type="xs:date" minOccurs="0" />
                        <xs:element name="period" type="xs:duration" minOccurs="1" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="specificTime" type="xs:date" />
        </xs:choice>
    </xs:complexType>

	<!-- <xs:assert test="not(validity/timeAfterDispatch) or validity/timeAfterDispatch/dispatchDate = //digitalSignature/date">
    Dispatch date in validity must match the signature date.
	</xs:assert> -->


    <xs:complexType name="materialClassType">
        <xs:sequence>
            <xs:element name="reference" type="dcc:notEmptyStringType" />
            <xs:element name="classID" type="dcc:notEmptyStringType" />
            <xs:element name="link" type="xs:anyURI" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="optional" />
        <xs:attribute name="refId" type="xs:IDREFS" use="optional" />
        <xs:attribute name="refType" type="dcc:refTypesType" use="optional" />
    </xs:complexType>


	<!-- <materialClass>
	<reference>
	</materialClass> -->



	<xs:complexType name="identificationListType">
		<xs:annotation>
			<xs:documentation> List of additional identifications. </xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="identification"
			            type="drmd:identificationType"
			            maxOccurs="unbounded"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="identificationType">
		<xs:annotation>
			<xs:documentation> An additional identification (eg. reference no., serial number,
				etc.). </xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="issuer">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<!-- <xs:enumeration value="manufacturer"/> -->
						<xs:enumeration value="referenceMaterialProducer"/>
						<xs:enumeration value="customer"/>
						<xs:enumeration value="owner"/>
						<xs:enumeration value="other"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:element>
			<xs:element name="value"
			            type="dcc:notEmptyStringType"/>
			<xs:element name="name"
			            type="dcc:textType"
			            minOccurs="0"/>
		</xs:sequence>
		<xs:attribute name="id"
		              type="xs:ID"
		              use="optional"/>
		<xs:attribute name="refType"
		              type="dcc:refTypesType"
		              use="optional"/>
	</xs:complexType>

	<xs:complexType name="referenceMaterialProducerType">
		<xs:annotation>
			<xs:documentation> Contains information about the producer of the reference material,
				including contact details. </xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="name"
			            type="dcc:textType"/>
			<xs:element name="contact"
			            type="dcc:contactType"/>
			<xs:element name="cryptElectronicSeal"
			            type="xs:boolean"
			            minOccurs="0"/>
			<xs:element name="cryptElectronicSignature"
			            type="xs:boolean"
			            minOccurs="0"/>
                        <xs:element name="cryptElectronicTimeStamp"
                                    type="xs:boolean"
                                    minOccurs="0"/>
                        <xs:element name="organizationIdentifiers"
                                    type="drmd:organizationIdentifierListType"
                                    minOccurs="0"/>
                </xs:sequence>
        </xs:complexType>
    
    <!-- materialListType moved to root as <materials> -->
    <xs:complexType name="materialListType">
        <xs:annotation>
            <xs:documentation>
                A list of materials included in the DRMD, each with details.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="material"
                        type="drmd:materialType"
                        maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <!-- materialType now includes materialClass as a subelement -->
    <xs:complexType name="materialType">
        <xs:annotation>
            <xs:documentation>
                Details about an individual material in the DRMD, including
                name, description, class, and sample sizes.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="name" type="dcc:textType"/>
            <xs:element name="description"
                        type="dcc:richContentType"
                        minOccurs="0"/>
            <xs:element name="materialClass"
                        type="drmd:materialClassType"
                        minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="minimumSampleSize"
                        type="dcc:itemQuantityListType"/>
            <xs:element name="itemQuantities"
                        type="dcc:itemQuantityListType"
                        minOccurs="0"/>
            <xs:element name="materialIdentifiers"
                        type="drmd:materialIdentifierListType"
                        minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>


	<xs:complexType name="statementListType">
		<xs:annotation>
			<xs:documentation> A list of statements related to the DRMD, including intended use,
				handling instructions, and storage information. </xs:documentation>
		</xs:annotation>
		<xs:sequence>
			<xs:element name="intendedUse"
			            type="dcc:richContentType"
			            minOccurs="1"/>
			<xs:element name="commutability"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="storageInformation"
			            type="dcc:richContentType"
			            minOccurs="1"/>
			<xs:element name="instructionsForHandlingAndUse"
			            type="dcc:richContentType"
			            minOccurs="1"/>
			<xs:element name="metrologicalTraceability"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="healthAndSafetyInformation"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="subcontractors"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="legalNotice"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="referenceToCertificationReport"
			            type="dcc:richContentType"
			            minOccurs="0"/>
			<xs:element name="statement"
			            type="dcc:richContentType"
			            minOccurs="0"
			            maxOccurs="unbounded"/>
		</xs:sequence>
	</xs:complexType>

  <xs:complexType name="materialPropertiesListType">
        <xs:annotation>
            <xs:documentation> List of measurement results that are part of a DRMD. </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="materialProperties"
                        type="drmd:materialPropertiesType"
                        maxOccurs="unbounded"/>
        </xs:sequence>
  </xs:complexType>

  <xs:complexType name="resultListType">
      <xs:sequence>
          <xs:element name="result" type="drmd:resultType" maxOccurs="unbounded"/>
      </xs:sequence>
  </xs:complexType>

    <xs:complexType name="resultType">
            <xs:annotation>
                <xs:documentation>
                    DRMD-specific result container. Elements are re-declared in the drmd namespace so that
                    all nested quantities resolve to drmd:quantityType (with propertyIdentifiers). Future
                    specialization could extend this base (e.g. certifiedResultType).
                </xs:documentation>
            </xs:annotation>
            <xs:sequence>
                    <xs:element name="name" type="dcc:textType" />
                    <xs:element name="description" type="dcc:richContentType" minOccurs="0" />
                    <xs:element name="data" type="drmd:dataType" />
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="optional" />
            <xs:attribute name="refId" type="xs:IDREFS" use="optional" />
            <xs:attribute name="refType" type="drmd:refTypesType" use="optional" />
    </xs:complexType>

  <!-- Option A: permit either a list of quantities or a single quantity -->
  <xs:complexType name="dataType">
      <xs:choice>
          <xs:element name="list" type="drmd:listType" />
          <xs:element name="quantity" type="drmd:quantityType" />
      </xs:choice>
  </xs:complexType>

  <xs:complexType name="listType">
      <xs:sequence>
          <xs:element name="quantity" type="drmd:quantityType" maxOccurs="unbounded"/>
      </xs:sequence>
  </xs:complexType>



    <xs:complexType name="materialPropertiesType">
        <xs:annotation>
            <xs:documentation> Container for used methods, used software, influence conditions,
                measuring equipment used for the measurments, metadata and measurments results. </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="name" type="dcc:textType" />
            <xs:element name="description" type="dcc:richContentType" minOccurs="0" />
            <xs:element name="procedures" type="dcc:usedMethodListType" minOccurs="0" />
            <xs:element name="results" type="drmd:resultListType" />
            <xs:element name="measurementMetaData" type="dcc:measurementMetaDataListType"
                minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="optional" />
        <xs:attribute name="refId" type="xs:IDREFS" use="optional" />
        <xs:attribute name="refType" type="dcc:refTypesType" use="optional" />
		<xs:attribute name="isCertified" type="xs:boolean" use="optional"/>




    </xs:complexType>



</xs:schema>
