<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:wales.nhs.uk/nwis/isd/datareturn"
    targetNamespace="urn:wales.nhs.uk/nwis/isd/datareturn" elementFormDefault="qualified"
    attributeFormDefault="unqualified" version="0.2">
    <xs:annotation>
        <xs:documentation>
      This schema document contains element declarations describing the whole
      data set and the data entities it contains.
    </xs:documentation>
        <xs:documentation>
      v0-1: Initial drafting from Substance Misuse Data Set Business Definition
      document dated 20130926
    </xs:documentation>
        <xs:documentation>
      v0-2: Structure revised following peer review; updateStatus added for
      entire ClientReferral and for AssessmentDetails, TreatmentModalityDetails and
      TreatmentOutcomeProfileDetails; minor revisions to datatypes.
    </xs:documentation>
        <xs:documentation>
      v0-3: Split out Referral Details into seperate element. Allow non-key fields to be empty.
    </xs:documentation>
    <xs:documentation>
      v0-4: Changes to reflect Substance Misuse Data Set Business Definition 0.5
    </xs:documentation>      
    </xs:annotation>
    <!-- Known issues for v0-3:  
        1) Documentation missing on most element declarations. See "DaysAlcoholUsed" in the data components schema document 
        for an example of documentation, to complete for other elements when time permits. 
    -->
    <xs:include schemaLocation="SubstanceMisuseDataReturnComponents-v0-4.xsd"/>
    <xs:element name="SubstanceMisuseReturn">
        <xs:annotation>
            <xs:documentation>
        The ClientReferral element is the fundamental building block of the
        dataset as represented in XML. Each ClientReferral element provides new, additional
        or updated information about an instance of the end-to-end business process from
        referral to discharge that is illustrated in the Business Definition document. The
        mandatory elements of the ReferralDetails element provide key information for data
        collation by NWIS across successive returns.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="ClientReferral" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ClientReferral">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>
          Each ClientReferral element has an update status as follows:
          "add" - new client referral; "update" - information concerning a client referral
          for which information has already been given in a previous return instance;
          "delete" - erroneous client referral to be removed from the cumulated dataset.
          The mandatory elements of ReferralDetails together form a key that is used to
          check for uniqueness of a client referral. A ClientReferral element with "add"
          or "update" may be followed (immediately or later in the return) by a
          ClientReferral with "update".
        </xs:documentation>
            </xs:annotation>
            <xs:sequence>
                <xs:element ref="ReferralDetails" minOccurs="1" maxOccurs="1"/>
                <xs:element ref="ClientDetails" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="AssessmentDetails" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="TreatmentModalityDetails" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="TreatmentOutcomeProfileDetails" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="DischargeDetails" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="updateStatus" type="updateStatusType"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="ReferralDetails">
        <xs:annotation>
            <xs:documentation>
        The required elements of the ReferralDetails element provide the key
        information for data collation by NWIS across successive returns. The optional
        elements constitute Discharge details and are populated when information is
        available about the closure of the client referral.
      </xs:documentation>
            <xs:documentation>
        Note that there is no separate "updateStatus" on the ReferralDetails
        element, since it provides identifying details for the overall
        ClientReferral.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="AgencyCode"/>
                <xs:element ref="AgencyClientNumber"/>
                <xs:element ref="DateOfReferral"/>
                <xs:element ref="SourceOfReferral"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ClientDetails">
        <xs:complexType>
            <xs:sequence>
                <xs:annotation>
                    <xs:documentation>
            When present, the ClientDetails element is used to update the
            client details associated with this ClientReferral. All items are required,
            however the NHS number may be empty if not known.
          </xs:documentation>
                </xs:annotation>
                <xs:element ref="NHSnumber"/>
                <xs:element ref="SurnameInitial"/>
                <xs:element ref="ForenameInitial"/>
                <xs:element ref="DateOfBirth"/>
                <xs:element ref="Gender"/>
                <xs:element ref="EthnicCategory"/>
                <xs:element ref="ShortPostcode"/>
                <xs:element ref="LocalAuthority"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="AssessmentDetails">
        <xs:annotation>
            <xs:documentation>
        Update status on each AssessmentDetails element may be as follows:
        "add" - New assessment; "update" - information concerning an assessment for which
        information has already been given in a previous return instance; "delete" -
        erroneous data to be removed from the cumulated dataset. The value of
        AssessmentDate is used to match an "update" or "delete" with previously
        returned AssessmentDetails. An AssessmentDetails element element with "delete" may
        be followed (immediately or later in the return) by an AssessmentDetails element
        with "add" that replaces the erroneous data just deleted.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="AssessmentDate"/>
                <xs:element ref="PreviouslyTreated"/>
                <xs:element ref="ExServicesPersonnel"/>
                <xs:element ref="ParentalResponsibility"/>
                <xs:element ref="ChildrenLivingInHousehold"/>
                <xs:element ref="ProblemSubstance1"/>
                <xs:element ref="ProblemSubstance2"/>
                <xs:element ref="ProblemSubstance3"/>
                <xs:element ref="InjectingStatus"/>
                <xs:element ref="HepatitisB-VacStatus"/>
                <xs:element ref="BBV-VacStatus"/>
                <xs:element ref="MentalHealthIssues"/>
                <xs:element ref="AccommodationNeed"/>
                <xs:element ref="EmploymentStatus"/>
            </xs:sequence>
            <xs:attribute name="updateStatus" type="updateStatusType"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="TreatmentModalityDetails">
        <xs:annotation>
            <xs:documentation>
        Update status on each TreatmentModalityDetails element may be as
        follows: "add" - New treatment modality details; "update" - information concerning a
        treatment modality for which information has already been given in a previous return
        instance; "delete" - erroneous data to be removed from the cumulated dataset. The
        combined values of TreatmentModalityKind and ModalityReferralDate are used to
        match an "update" or "delete" with previously returned TreatmentModalityDetails. A
        TreatmentModalityDetails element with "delete" may be followed (immediately or later
        in the return) by a TreatmentModalityDetails element with "add" that replaces the
        erroneous data just deleted.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="TreatmentModalityKind"/>
                <xs:element ref="ModalityReferralDate"/>
                <xs:element ref="ModalityFirstApptOfferedDate"/>
                <xs:element ref="ModalityStartDate"/>
                <xs:element ref="ModalityEndDate"/>
                <xs:element ref="ModalityExitStatus"/>
            </xs:sequence>
            <xs:attribute name="updateStatus" type="updateStatusType"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="TreatmentOutcomeProfileDetails">
        <xs:annotation>
            <xs:documentation>
        Update status on each TreatmentOutcomeProfileDetails element may be
        as follows: "add" - New TOP details; "update" - information concerning a TOP for
        which information has already been given in a previous return instance; "delete" -
        erroneous data to be removed from the cumulated dataset. The combined values of
        TOPnumber and TOPinterviewDate are used to match an "update" or "delete" with
        previously returned TreatmentOutcomeProfileDetails. A TreatmentOutcomeProfileDetails
        element with "delete" may be followed (immediately or later in the return) by a
        TreatmentOutcomeProfileDetails element with "add" that replaces the erroneous data
        just deleted.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="TOPnumber"/>
                <xs:element ref="TOPinterviewDate"/>
                <xs:element ref="TreatmentStage"/>
                <xs:element ref="DaysAlcoholUsed"/>
                <xs:element ref="DaysOpiateUsed"/>
                <xs:element ref="DaysCrackUsed"/>
                <xs:element ref="DaysCocaineUsed"/>
                <xs:element ref="DaysAmphetaminesUsed"/>
                <xs:element ref="DaysCannabisUsed"/>
                <xs:element ref="OtherProblemSubstance"/>
                <xs:element ref="DaysOtherSubstanceUsed"/>
                <xs:element ref="DaysInjected"/>
                <xs:element ref="InjectedSharedSharp"/>
                <xs:element ref="DaysShoplifting"/>
                <xs:element ref="DaysDrugSelling"/>
                <xs:element ref="CriminalOffenceStatus"/>
                <xs:element ref="AssaultOrViolence"/>
                <xs:element ref="PsychologicalHealthStatus"/>
                <xs:element ref="DaysPaidWork"/>
                <xs:element ref="DaysCollegeOrSchool"/>
                <xs:element ref="PhysicalHealthStatus"/>
                <xs:element ref="UrgentHousingProblemStatus"/>
                <xs:element ref="RiskOfEvictionStatus"/>
                <xs:element ref="QualityOfLifeStatus"/>
            </xs:sequence>
            <xs:attribute name="updateStatus" type="updateStatusType"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="DischargeDetails">
        <xs:annotation>
            <xs:documentation>
        Update status on each DischargeDetails element may be as
        follows: "add" - New treatment modality details; "update" - information concerning a
        treatment modality for which information has already been given in a previous return
        instance; "delete" - erroneous data to be removed from the cumulated dataset. A
        DischargeDetails element with "delete" may be followed (immediately or later
        in the return) by a DischargeDetails element with "add" that replaces the
        erroneous data just deleted.
      </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="DateContactEnded"/>
                <xs:element ref="ReasonContactEnded"/>
            </xs:sequence>
            <xs:attribute name="updateStatus" type="updateStatusType"/>
        </xs:complexType>
    </xs:element>
</xs:schema>
