<?xml version="1.0" encoding="UTF-8"?>
<!--
    Canonical schema for the patient-query XML grammar parsed by
    PatientQueryDefinition (backend/src/CareRecord/Infrastructure/PatientQuery/).

    Covers every variable tag parsed by extractVariables() (29 tags), every
    <codeList> payload shape parsed by extractCodeLists(), and the full/standalone
    <patientQuery> envelope (population, codeLists, variables, rules) used by
    on-disk queries under .../PatientQuery/queries/ and by future-action-rule.xsd's
    inline <targetPatientQuery><patientQuery>.

    Strict about vocabulary, deliberately loose about shape (see
    shipped-report.xsd for the same precedent): an element or attribute name that
    is not part of the language is rejected, but ordering and repetition are not
    policed, and required-ness of top-level elements (e.g. <rules> for a full
    query) is left to PatientQueryDefinition's own runtime validation rather than
    encoded here.

    Comma-separated attribute values (e.g. select="a,b,c") are matched with
    xs:pattern rather than xs:list, because XSD's xs:list only supports
    whitespace-separated items.

    Out of scope: <logic>/<returnValueFieldName> (PARTIAL_REPORT_PATIENT_QUERY
    report-embedded fragments) belong to shipped-report.xsd/custom-report schema
    territory, not this file.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- ===== Single-value enumerations ===== -->

    <xs:simpleType name="takeMethodType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="latest"/>
            <xs:enumeration value="earliest"/>
            <xs:enumeration value="all"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="fetchMethodType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="latest"/>
            <xs:enumeration value="earliest"/>
            <xs:enumeration value="all"/>
            <xs:enumeration value="count"/>
            <xs:enumeration value="associated"/>
            <xs:enumeration value="all_merge"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="appointmentColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="id"/>
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="status"/>
            <xs:enumeration value="practitioner_identifier_type"/>
            <xs:enumeration value="practitioner_identifier_value"/>
            <xs:enumeration value="practitioner_name"/>
            <xs:enumeration value="appointment_type_id"/>
            <xs:enumeration value="appointment_type_name"/>
            <xs:enumeration value="appointment_service_id"/>
            <xs:enumeration value="appointment_service_name"/>
            <xs:enumeration value="nhs_national_slot_type_category"/>
            <xs:enumeration value="intended_start_date_time"/>
            <xs:enumeration value="intended_end_date_time"/>
            <xs:enumeration value="intended_duration_seconds"/>
            <xs:enumeration value="appointment_container_id"/>
            <xs:enumeration value="delivery_mode"/>
            <xs:enumeration value="linked_encounter_id"/>
            <xs:enumeration value="arrived_date_time"/>
            <xs:enumeration value="booked_date_time"/>
            <xs:enumeration value="cancelled_date_time"/>
            <xs:enumeration value="rescheduled_date_time"/>
            <xs:enumeration value="appointment_site_id"/>
            <xs:enumeration value="appointment_room_id"/>
            <xs:enumeration value="appointment_site_name"/>
            <xs:enumeration value="appointment_room_name"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="uid"/>
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="type"/>
            <xs:enumeration value="status"/>
            <xs:enumeration value="product_code"/>
            <xs:enumeration value="product_description"/>
            <xs:enumeration value="dosage_instruction"/>
            <xs:enumeration value="start_date"/>
            <xs:enumeration value="expected_end_date"/>
            <xs:enumeration value="authorised_date"/>
            <xs:enumeration value="authorised_by_practitioner_identifier_type"/>
            <xs:enumeration value="authorised_by_practitioner_identifier_value"/>
            <xs:enumeration value="authorised_by_practitioner_name"/>
            <xs:enumeration value="last_issue_date"/>
            <xs:enumeration value="discontinued_date"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionIssueColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="id"/>
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="prescription_id"/>
            <xs:enumeration value="product_code"/>
            <xs:enumeration value="product_description"/>
            <xs:enumeration value="expected_days_supply"/>
            <xs:enumeration value="supply_end_date"/>
            <xs:enumeration value="quantity_value"/>
            <xs:enumeration value="quantity_unit_code"/>
            <xs:enumeration value="quantity_unit_description"/>
            <xs:enumeration value="issue_date"/>
            <xs:enumeration value="issue_method"/>
            <xs:enumeration value="issued_by_practitioner_identifier_type"/>
            <xs:enumeration value="issued_by_practitioner_identifier_value"/>
            <xs:enumeration value="issued_by_practitioner_name"/>
            <xs:enumeration value="issued_by_organisation"/>
            <xs:enumeration value="issue_number"/>
            <xs:enumeration value="issue_status"/>
            <xs:enumeration value="pharmacy_ods_code"/>
            <xs:enumeration value="prescription_dosage_instruction"/>
            <xs:enumeration value="prescription_type"/>
            <xs:enumeration value="prescription_status"/>
            <xs:enumeration value="prescription_authorised_date"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="patientRegistrationColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="id"/>
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="start_date"/>
            <xs:enumeration value="end_date"/>
            <xs:enumeration value="registration_type"/>
            <xs:enumeration value="prescriptions_dispensed_by_practice"/>
            <xs:enumeration value="care_home_resident"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalCaseColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="case_status"/>
            <xs:enumeration value="created_date_time"/>
            <xs:enumeration value="closed_date_time"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalCaseColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(patient_id|case_status|created_date_time|closed_date_time)\s*(,\s*(patient_id|case_status|created_date_time|closed_date_time)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalCaseStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(open|closed)\s*(,\s*(open|closed)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalPathwayColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="pathway_status"/>
            <xs:enumeration value="created_date_time"/>
            <xs:enumeration value="removed_date_time"/>
            <xs:enumeration value="pathway_name"/>
            <xs:enumeration value="current_stage_name"/>
            <xs:enumeration value="current_stage_position"/>
            <xs:enumeration value="clinical_case_id"/>
            <xs:enumeration value="case_type_name"/>
            <xs:enumeration value="commissioned_service_name"/>
            <xs:enumeration value="commissioning_organisation_name"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalPathwayColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(patient_id|pathway_status|created_date_time|removed_date_time|pathway_name|current_stage_name|current_stage_position|clinical_case_id|case_type_name|commissioned_service_name|commissioning_organisation_name)\s*(,\s*(patient_id|pathway_status|created_date_time|removed_date_time|pathway_name|current_stage_name|current_stage_position|clinical_case_id|case_type_name|commissioned_service_name|commissioning_organisation_name)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalPathwayStageChangeColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="changed_date_time"/>
            <xs:enumeration value="from_stage_name"/>
            <xs:enumeration value="to_stage_name"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalPathwayStageChangeColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(patient_id|changed_date_time|from_stage_name|to_stage_name)\s*(,\s*(patient_id|changed_date_time|from_stage_name|to_stage_name)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="clinicalPathwayStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(active|removed)\s*(,\s*(active|removed)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryColumnType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="uid"/>
            <xs:enumeration value="care_record_id"/>
            <xs:enumeration value="patient_id"/>
            <xs:enumeration value="entity_type"/>
            <xs:enumeration value="code"/>
            <xs:enumeration value="code_description"/>
            <xs:enumeration value="clinically_relevant_date"/>
            <xs:enumeration value="clinically_relevant_time"/>
            <xs:enumeration value="care_record_sorting_date_time"/>
            <xs:enumeration value="created_in_original_system_date_time"/>
            <xs:enumeration value="created_date_time"/>
            <xs:enumeration value="end_date"/>
            <xs:enumeration value="status"/>
            <xs:enumeration value="encounter_id"/>
            <xs:enumeration value="value_1"/>
            <xs:enumeration value="value_1_unit_code"/>
            <xs:enumeration value="value_1_unit_description"/>
            <xs:enumeration value="value_2"/>
            <xs:enumeration value="value_2_unit_code"/>
            <xs:enumeration value="value_2_unit_description"/>
            <xs:enumeration value="body_site_code"/>
            <xs:enumeration value="body_site_description"/>
            <xs:enumeration value="route_code"/>
            <xs:enumeration value="route_description"/>
            <xs:enumeration value="batch_number"/>
            <xs:enumeration value="expiry_date"/>
            <xs:enumeration value="practitioner_name"/>
            <xs:enumeration value="practitioner_identifier_type"/>
            <xs:enumeration value="practitioner_identifier_value"/>
            <xs:enumeration value="organisation_identifier_type"/>
            <xs:enumeration value="organisation_identifier_value"/>
            <xs:enumeration value="performed_at_local_organisation"/>
            <xs:enumeration value="organisation_name"/>
            <xs:enumeration value="encounter_type_code"/>
            <xs:enumeration value="encounter_type_description"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryStatusType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="active"/>
            <xs:enumeration value="ended"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="issueMethodType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="electronic-prescription-service"/>
            <xs:enumeration value="fp10ss-printed-script"/>
            <xs:enumeration value="handwritten-script"/>
            <xs:enumeration value="private-handwritten-script"/>
            <xs:enumeration value="private-printed-script"/>
            <xs:enumeration value="fp10-personally-administered"/>
            <xs:enumeration value="fp10-private-controlled-drug"/>
            <xs:enumeration value="gg-ps2016-printed-script"/>
            <xs:enumeration value="gg-private-printed-script"/>
            <xs:enumeration value="local-dispensary"/>
            <xs:enumeration value="unknown"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="issueStatusType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="not-issued"/>
            <xs:enumeration value="issued"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="patientRegistrationTypeCategoryType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="immediate-necessary-treatment"/>
            <xs:enumeration value="temporary"/>
            <xs:enumeration value="permanent"/>
            <xs:enumeration value="custom"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionTypeType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="acute"/>
            <xs:enumeration value="repeat"/>
            <xs:enumeration value="variable-repeat"/>
            <xs:enumeration value="repeat-dispensing"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionStatusType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="authorised"/>
            <xs:enumeration value="discontinued"/>
            <xs:enumeration value="reauthorised"/>
            <xs:enumeration value="requires-review"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryEntityTypeType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="investigation-group"/>
            <xs:enumeration value="investigation-result"/>
            <xs:enumeration value="fit-note"/>
            <xs:enumeration value="note"/>
            <xs:enumeration value="outbound-referral"/>
            <xs:enumeration value="allergy-intolerance"/>
            <xs:enumeration value="communication"/>
            <xs:enumeration value="document"/>
            <xs:enumeration value="consultation-topic"/>
            <xs:enumeration value="immunisation"/>
            <xs:enumeration value="medication-statement"/>
            <xs:enumeration value="observation"/>
            <xs:enumeration value="prescription"/>
            <xs:enumeration value="problem"/>
            <xs:enumeration value="procedure"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="appointmentStatusType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="pending"/>
            <xs:enumeration value="started"/>
            <xs:enumeration value="seen"/>
            <xs:enumeration value="rescheduled"/>
            <xs:enumeration value="cancelled"/>
            <xs:enumeration value="did-not-attend"/>
            <xs:enumeration value="walked-out"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="deliveryModeType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="face-to-face"/>
            <xs:enumeration value="home-visit"/>
            <xs:enumeration value="video"/>
            <xs:enumeration value="phone"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="nhsNationalSlotTypeCategoryType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="10101"/>
            <xs:enumeration value="10102"/>
            <xs:enumeration value="10103"/>
            <xs:enumeration value="10104"/>
            <xs:enumeration value="10105"/>
            <xs:enumeration value="10106"/>
            <xs:enumeration value="10107"/>
            <xs:enumeration value="10108"/>
            <xs:enumeration value="10109"/>
            <xs:enumeration value="10110"/>
            <xs:enumeration value="10111"/>
            <xs:enumeration value="10112"/>
            <xs:enumeration value="10113"/>
            <xs:enumeration value="10114"/>
            <xs:enumeration value="10115"/>
            <xs:enumeration value="10116"/>
            <xs:enumeration value="10117"/>
            <xs:enumeration value="10118"/>
            <xs:enumeration value="10119"/>
            <xs:enumeration value="10120"/>
            <xs:enumeration value="10121"/>
            <xs:enumeration value="10122"/>
            <xs:enumeration value="10123"/>
            <xs:enumeration value="10124"/>
            <xs:enumeration value="10125"/>
            <xs:enumeration value="10126"/>
            <xs:enumeration value="10127"/>
            <xs:enumeration value="10128"/>
            <xs:enumeration value="10129"/>
            <xs:enumeration value="10130"/>
            <xs:enumeration value="10131"/>
            <xs:enumeration value="10132"/>
            <xs:enumeration value="10133"/>
            <xs:enumeration value="10134"/>
            <xs:enumeration value="10135"/>
            <xs:enumeration value="10136"/>
            <xs:enumeration value="10137"/>
            <xs:enumeration value="10138"/>
            <xs:enumeration value="20101"/>
            <xs:enumeration value="20102"/>
            <xs:enumeration value="20103"/>
            <xs:enumeration value="20104"/>
            <xs:enumeration value="20105"/>
            <xs:enumeration value="20106"/>
            <xs:enumeration value="20107"/>
            <xs:enumeration value="20108"/>
            <xs:enumeration value="20109"/>
            <xs:enumeration value="20110"/>
            <xs:enumeration value="20111"/>
            <xs:enumeration value="20112"/>
            <xs:enumeration value="20113"/>
            <xs:enumeration value="20114"/>
            <xs:enumeration value="20115"/>
            <xs:enumeration value="20116"/>
            <xs:enumeration value="20117"/>
            <xs:enumeration value="20118"/>
            <xs:enumeration value="20119"/>
            <xs:enumeration value="20120"/>
            <xs:enumeration value="20121"/>
            <xs:enumeration value="20122"/>
            <xs:enumeration value="20123"/>
            <xs:enumeration value="20124"/>
            <xs:enumeration value="20125"/>
            <xs:enumeration value="20126"/>
            <xs:enumeration value="20127"/>
            <xs:enumeration value="20128"/>
            <xs:enumeration value="20129"/>
            <xs:enumeration value="20130"/>
            <xs:enumeration value="20131"/>
            <xs:enumeration value="20132"/>
            <xs:enumeration value="20133"/>
            <xs:enumeration value="20134"/>
            <xs:enumeration value="20135"/>
            <xs:enumeration value="20136"/>
            <xs:enumeration value="20137"/>
            <xs:enumeration value="20138"/>
            <xs:enumeration value="30101"/>
            <xs:enumeration value="30102"/>
            <xs:enumeration value="30103"/>
            <xs:enumeration value="30104"/>
            <xs:enumeration value="30105"/>
            <xs:enumeration value="30106"/>
            <xs:enumeration value="30107"/>
            <xs:enumeration value="30108"/>
            <xs:enumeration value="30109"/>
            <xs:enumeration value="30110"/>
            <xs:enumeration value="30111"/>
            <xs:enumeration value="30112"/>
            <xs:enumeration value="30113"/>
            <xs:enumeration value="30114"/>
            <xs:enumeration value="30115"/>
            <xs:enumeration value="30116"/>
            <xs:enumeration value="30117"/>
            <xs:enumeration value="30118"/>
            <xs:enumeration value="30119"/>
            <xs:enumeration value="30120"/>
            <xs:enumeration value="30121"/>
            <xs:enumeration value="30122"/>
            <xs:enumeration value="30123"/>
            <xs:enumeration value="30124"/>
            <xs:enumeration value="30125"/>
            <xs:enumeration value="30126"/>
            <xs:enumeration value="30127"/>
            <xs:enumeration value="30128"/>
            <xs:enumeration value="30129"/>
            <xs:enumeration value="30130"/>
            <xs:enumeration value="30131"/>
            <xs:enumeration value="30132"/>
            <xs:enumeration value="30133"/>
            <xs:enumeration value="30134"/>
            <xs:enumeration value="30135"/>
            <xs:enumeration value="30136"/>
            <xs:enumeration value="30137"/>
            <xs:enumeration value="30138"/>
            <xs:enumeration value="40101"/>
            <xs:enumeration value="40102"/>
            <xs:enumeration value="40103"/>
            <xs:enumeration value="40104"/>
            <xs:enumeration value="40105"/>
            <xs:enumeration value="40106"/>
            <xs:enumeration value="40107"/>
            <xs:enumeration value="40108"/>
            <xs:enumeration value="40109"/>
            <xs:enumeration value="40110"/>
            <xs:enumeration value="40111"/>
            <xs:enumeration value="40112"/>
            <xs:enumeration value="40113"/>
            <xs:enumeration value="40114"/>
            <xs:enumeration value="40115"/>
            <xs:enumeration value="40116"/>
            <xs:enumeration value="40117"/>
            <xs:enumeration value="40118"/>
            <xs:enumeration value="40119"/>
            <xs:enumeration value="40120"/>
            <xs:enumeration value="40121"/>
            <xs:enumeration value="40122"/>
            <xs:enumeration value="40123"/>
            <xs:enumeration value="40124"/>
            <xs:enumeration value="40125"/>
            <xs:enumeration value="40126"/>
            <xs:enumeration value="40127"/>
            <xs:enumeration value="40128"/>
            <xs:enumeration value="40129"/>
            <xs:enumeration value="40140"/>
            <xs:enumeration value="40131"/>
            <xs:enumeration value="40132"/>
            <xs:enumeration value="40133"/>
            <xs:enumeration value="40134"/>
            <xs:enumeration value="40135"/>
            <xs:enumeration value="40136"/>
            <xs:enumeration value="40137"/>
            <xs:enumeration value="40138"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- ===== Comma-separated list variants (xs:list only supports whitespace separators) ===== -->

    <xs:simpleType name="appointmentColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(id|patient_id|status|practitioner_identifier_type|practitioner_identifier_value|practitioner_name|appointment_type_id|appointment_type_name|appointment_service_id|appointment_service_name|nhs_national_slot_type_category|intended_start_date_time|intended_end_date_time|intended_duration_seconds|appointment_container_id|delivery_mode|linked_encounter_id|arrived_date_time|booked_date_time|cancelled_date_time|rescheduled_date_time|appointment_site_id|appointment_room_id|appointment_site_name|appointment_room_name)\s*(,\s*(id|patient_id|status|practitioner_identifier_type|practitioner_identifier_value|practitioner_name|appointment_type_id|appointment_type_name|appointment_service_id|appointment_service_name|nhs_national_slot_type_category|intended_start_date_time|intended_end_date_time|intended_duration_seconds|appointment_container_id|delivery_mode|linked_encounter_id|arrived_date_time|booked_date_time|cancelled_date_time|rescheduled_date_time|appointment_site_id|appointment_room_id|appointment_site_name|appointment_room_name)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(uid|patient_id|type|status|product_code|product_description|dosage_instruction|start_date|expected_end_date|authorised_date|authorised_by_practitioner_identifier_type|authorised_by_practitioner_identifier_value|authorised_by_practitioner_name|last_issue_date|discontinued_date)\s*(,\s*(uid|patient_id|type|status|product_code|product_description|dosage_instruction|start_date|expected_end_date|authorised_date|authorised_by_practitioner_identifier_type|authorised_by_practitioner_identifier_value|authorised_by_practitioner_name|last_issue_date|discontinued_date)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionIssueColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(id|patient_id|prescription_id|product_code|product_description|expected_days_supply|supply_end_date|quantity_value|quantity_unit_code|quantity_unit_description|issue_date|issue_method|issued_by_practitioner_identifier_type|issued_by_practitioner_identifier_value|issued_by_practitioner_name|issued_by_organisation|issue_number|issue_status|pharmacy_ods_code|prescription_dosage_instruction|prescription_type|prescription_status|prescription_authorised_date)\s*(,\s*(id|patient_id|prescription_id|product_code|product_description|expected_days_supply|supply_end_date|quantity_value|quantity_unit_code|quantity_unit_description|issue_date|issue_method|issued_by_practitioner_identifier_type|issued_by_practitioner_identifier_value|issued_by_practitioner_name|issued_by_organisation|issue_number|issue_status|pharmacy_ods_code|prescription_dosage_instruction|prescription_type|prescription_status|prescription_authorised_date)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- An issue's own columns, the only ones lastIssueFields can read. -->
    <xs:simpleType name="prescriptionLastIssueColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(id|patient_id|prescription_id|product_code|product_description|expected_days_supply|supply_end_date|quantity_value|quantity_unit_code|quantity_unit_description|issue_date|issue_method|issued_by_practitioner_identifier_type|issued_by_practitioner_identifier_value|issued_by_practitioner_name|issued_by_organisation|issue_number|issue_status|pharmacy_ods_code)\s*(,\s*(id|patient_id|prescription_id|product_code|product_description|expected_days_supply|supply_end_date|quantity_value|quantity_unit_code|quantity_unit_description|issue_date|issue_method|issued_by_practitioner_identifier_type|issued_by_practitioner_identifier_value|issued_by_practitioner_name|issued_by_organisation|issue_number|issue_status|pharmacy_ods_code)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="patientRegistrationColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(id|patient_id|start_date|end_date|registration_type|prescriptions_dispensed_by_practice|care_home_resident)\s*(,\s*(id|patient_id|start_date|end_date|registration_type|prescriptions_dispensed_by_practice|care_home_resident)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryColumnListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(uid|care_record_id|patient_id|entity_type|code|code_description|clinically_relevant_date|clinically_relevant_time|care_record_sorting_date_time|created_in_original_system_date_time|created_date_time|end_date|status|encounter_id|value_1|value_1_unit_code|value_1_unit_description|value_2|value_2_unit_code|value_2_unit_description|body_site_code|body_site_description|route_code|route_description|batch_number|expiry_date|practitioner_name|practitioner_identifier_type|practitioner_identifier_value|organisation_identifier_type|organisation_identifier_value|performed_at_local_organisation|organisation_name|encounter_type_code|encounter_type_description)\s*(,\s*(uid|care_record_id|patient_id|entity_type|code|code_description|clinically_relevant_date|clinically_relevant_time|care_record_sorting_date_time|created_in_original_system_date_time|created_date_time|end_date|status|encounter_id|value_1|value_1_unit_code|value_1_unit_description|value_2|value_2_unit_code|value_2_unit_description|body_site_code|body_site_description|route_code|route_description|batch_number|expiry_date|practitioner_name|practitioner_identifier_type|practitioner_identifier_value|organisation_identifier_type|organisation_identifier_value|performed_at_local_organisation|organisation_name|encounter_type_code|encounter_type_description)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(active|ended)\s*(,\s*(active|ended)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="issueMethodListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(electronic\-prescription\-service|fp10ss\-printed\-script|handwritten\-script|private\-handwritten\-script|private\-printed\-script|fp10\-personally\-administered|gg\-ps2016\-printed\-script|gg\-private\-printed\-script|local\-dispensary|unknown)\s*(,\s*(electronic\-prescription\-service|fp10ss\-printed\-script|handwritten\-script|private\-handwritten\-script|private\-printed\-script|fp10\-personally\-administered|gg\-ps2016\-printed\-script|gg\-private\-printed\-script|local\-dispensary|unknown)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="issueStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(not\-issued|issued)\s*(,\s*(not\-issued|issued)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="patientRegistrationTypeCategoryListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(immediate\-necessary\-treatment|temporary|permanent|custom)\s*(,\s*(immediate\-necessary\-treatment|temporary|permanent|custom)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionTypeListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(acute|repeat|variable\-repeat|repeat\-dispensing)\s*(,\s*(acute|repeat|variable\-repeat|repeat\-dispensing)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="prescriptionStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(authorised|discontinued|reauthorised|requires\-review)\s*(,\s*(authorised|discontinued|reauthorised|requires\-review)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="codedEntryEntityTypeListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(investigation\-group|investigation\-result|fit\-note|note|outbound\-referral|allergy\-intolerance|communication|document|consultation\-topic|immunisation|medication\-statement|observation|prescription|problem|procedure)\s*(,\s*(investigation\-group|investigation\-result|fit\-note|note|outbound\-referral|allergy\-intolerance|communication|document|consultation\-topic|immunisation|medication\-statement|observation|prescription|problem|procedure)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="appointmentStatusListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(pending|started|seen|rescheduled|cancelled|did\-not\-attend)\s*(,\s*(pending|started|seen|rescheduled|cancelled|did\-not\-attend)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="deliveryModeListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(face\-to\-face|home\-visit|video|phone)\s*(,\s*(face\-to\-face|home\-visit|video|phone)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="nhsNationalSlotTypeCategoryListType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\s*(10101|10102|10103|10104|10105|10106|10107|10108|10109|10110|10111|10112|10113|10114|10115|10116|10117|10118|10119|10120|10121|10122|10123|10124|10125|10126|10127|10128|10129|10130|10131|10132|10133|10134|10135|10136|10137|10138|20101|20102|20103|20104|20105|20106|20107|20108|20109|20110|20111|20112|20113|20114|20115|20116|20117|20118|20119|20120|20121|20122|20123|20124|20125|20126|20127|20128|20129|20130|20131|20132|20133|20134|20135|20136|20137|20138|30101|30102|30103|30104|30105|30106|30107|30108|30109|30110|30111|30112|30113|30114|30115|30116|30117|30118|30119|30120|30121|30122|30123|30124|30125|30126|30127|30128|30129|30130|30131|30132|30133|30134|30135|30136|30137|30138|40101|40102|40103|40104|40105|40106|40107|40108|40109|40110|40111|40112|40113|40114|40115|40116|40117|40118|40119|40120|40121|40122|40123|40124|40125|40126|40127|40128|40129|40140|40131|40132|40133|40134|40135|40136|40137|40138)\s*(,\s*(10101|10102|10103|10104|10105|10106|10107|10108|10109|10110|10111|10112|10113|10114|10115|10116|10117|10118|10119|10120|10121|10122|10123|10124|10125|10126|10127|10128|10129|10130|10131|10132|10133|10134|10135|10136|10137|10138|20101|20102|20103|20104|20105|20106|20107|20108|20109|20110|20111|20112|20113|20114|20115|20116|20117|20118|20119|20120|20121|20122|20123|20124|20125|20126|20127|20128|20129|20130|20131|20132|20133|20134|20135|20136|20137|20138|30101|30102|30103|30104|30105|30106|30107|30108|30109|30110|30111|30112|30113|30114|30115|30116|30117|30118|30119|30120|30121|30122|30123|30124|30125|30126|30127|30128|30129|30130|30131|30132|30133|30134|30135|30136|30137|30138|40101|40102|40103|40104|40105|40106|40107|40108|40109|40110|40111|40112|40113|40114|40115|40116|40117|40118|40119|40120|40121|40122|40123|40124|40125|40126|40127|40128|40129|40140|40131|40132|40133|40134|40135|40136|40137|40138)\s*)*"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- ===== Shared attribute group ===== -->

    <!--
        Every variable class extends PatientQueryElementBase, whose constructor reads
        two attributes from every tag: debug="true" enables per-patient debug
        logging (compared as a literal string, not cast through a real enum), and
        description is shown against the variable in the report debug modal.
    -->
    <xs:attributeGroup name="variableDebugAttributeGroup">
        <xs:attribute name="debug" type="xs:string" use="optional"/>
        <xs:attribute name="description" type="xs:string" use="optional"/>
    </xs:attributeGroup>

    <!-- A small closed set used by hand-parsed (not enum-backed) boolean-ish attributes. -->
    <xs:simpleType name="trueFalseAttributeType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="TRUE"/>
            <xs:enumeration value="FALSE"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- ===== authors ===== -->

    <xs:complexType name="authorsElementType">
        <xs:sequence>
            <xs:element name="author" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <!-- ===== codeLists =====

        PatientQueryDefinition::extractCodeLists() supports, per <codeList>:
          - <codeListReference>identifier</codeListReference>                          (v2)
          - <codeListDefinition><rules><inclusion>.../<exclusion>...</rules></codeListDefinition> (v2, inline SNOMED/DMD/ECL rules)
          - <fixedCodeList><codes><code>...</code></codes></fixedCodeList>             (v2, inline literal codes)
          - <reference>identifier</reference>, or a `reference` attribute             (v1, legacy)
        <name> may be a child element or a `name` attribute on <codeList> itself.
        Two v1-only siblings of <codeList> under <codeLists> also exist:
          - <definedCodeList><name>...</name><codes><code>...</code></codes></definedCodeList>
          - a top-level <codeListDefinition><name>...</name><rules>...</rules></codeListDefinition>
            (distinct from the nested-in-<codeList> form above, which has no own <name>).
    -->

    <xs:complexType name="codeListRulesElementType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="inclusion" type="xs:string"/>
            <xs:element name="exclusion" type="xs:string"/>
        </xs:choice>
    </xs:complexType>

    <!-- nested inside <codeList>: no own <name>, the name lives on the parent <codeList>. -->
    <xs:complexType name="codeListDefinitionElementType">
        <xs:sequence>
            <xs:element name="rules" type="codeListRulesElementType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <!-- top-level under <codeLists>, a sibling of <codeList>: carries its own <name>. -->
    <xs:complexType name="topLevelCodeListDefinitionElementType">
        <xs:sequence>
            <xs:element name="name" type="xs:string" minOccurs="0"/>
            <xs:element name="rules" type="codeListRulesElementType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="codesListElementType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="code" type="xs:string"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="fixedCodeListElementType">
        <xs:sequence>
            <xs:element name="codes" type="codesListElementType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <!-- mixed="true" tolerates incidental stray text (e.g. a copy-paste artifact seen in one
         real file); extractCodeLists() only ever reads named children/attributes, never text. -->
    <xs:complexType name="codeListElementType" mixed="true">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="name" type="xs:string"/>
            <xs:element name="reference" type="xs:string"/>
            <xs:element name="codeListReference" type="xs:string"/>
            <xs:element name="codeListDefinition" type="codeListDefinitionElementType"/>
            <xs:element name="fixedCodeList" type="fixedCodeListElementType"/>
        </xs:choice>
        <xs:attribute name="name" type="xs:string" use="optional"/>
        <xs:attribute name="reference" type="xs:string" use="optional"/>
    </xs:complexType>

    <xs:complexType name="definedCodeListElementType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="name" type="xs:string"/>
            <xs:element name="codes" type="codesListElementType"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="codeListsElementType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="codeList" type="codeListElementType"/>
            <xs:element name="definedCodeList" type="definedCodeListElementType"/>
            <xs:element name="codeListDefinition" type="topLevelCodeListDefinitionElementType"/>
        </xs:choice>
    </xs:complexType>

    <!-- ===== population =====

        Supports (parsePopulationXml()): a plain identifier as text content ("ALL",
        "nhs-england/AST_REG"), <patientReportReference>, <patientQueryReference>, a
        nested full <patientQuery> (recursive), or v2's <patientIdList><patientId>.
    -->

    <xs:complexType name="patientIdListElementType">
        <xs:sequence>
            <xs:element name="patientId" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="populationElementType" mixed="true">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="patientReportReference" type="xs:string"/>
            <xs:element name="patientQueryReference" type="xs:string"/>
            <xs:element name="patientQuery" type="patientQueryElementType"/>
            <xs:element name="patientIdList" type="patientIdListElementType"/>
        </xs:choice>
    </xs:complexType>

    <!-- ===== rules ===== -->

    <xs:complexType name="ruleElementType">
        <xs:sequence>
            <!-- Shown against the rule in the report debug modal. -->
            <xs:element name="description" type="xs:string" minOccurs="0"/>
            <xs:element name="if" type="xs:string"/>
            <xs:element name="then" type="xs:string"/>
            <xs:element name="else" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="rulesElementType">
        <xs:sequence>
            <xs:element name="rule" type="ruleElementType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <!-- ===== Variable types (29 tags, PatientQueryDefinition::extractVariables()) ===== -->

    <!-- fixedDate -->
    <xs:complexType name="fixedDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="runtimeValue" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- codedEntryDate -->
    <xs:complexType name="codedEntryDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
            <xs:attribute name="codeList" type="xs:string" use="optional"/>
            <xs:attribute name="logic" type="xs:string" use="optional"/>
            <xs:attribute name="codedEntryName" type="xs:string" use="optional"/>
            <xs:attribute name="recordedOn" type="xs:string" use="optional"/>
            <xs:attribute name="encounterTypeCode" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- codedEntryValue -->
    <xs:complexType name="codedEntryValueElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="onDate" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="xs:string" use="optional"/>
            <xs:attribute name="valueType" type="xs:string" use="optional"/>
            <!-- Present on real files but never read by PatientQueryCodedEntryValue. -->
            <xs:attribute name="codeList" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- appointmentVariable -->
    <xs:complexType name="appointmentVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="appointmentColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="order" type="appointmentColumnType" use="required"/>
            <xs:attribute name="status" type="appointmentStatusListType" use="optional"/>
            <xs:attribute name="practitioner" type="xs:string" use="optional"/>
            <xs:attribute name="practitionerIdentifierType" type="xs:string" use="optional"/>
            <xs:attribute name="practitionerIdentifierValue" type="xs:string" use="optional"/>
            <xs:attribute name="appointmentType" type="xs:string" use="optional"/>
            <xs:attribute name="appointmentService" type="xs:string" use="optional"/>
            <xs:attribute name="nhsNationalSlotTypeCategory" type="nhsNationalSlotTypeCategoryListType" use="optional"/>
            <xs:attribute name="deliveryMode" type="deliveryModeListType" use="optional"/>
            <xs:attribute name="intendedStartDateTime" type="xs:string" use="optional"/>
            <xs:attribute name="intendedEndDateTime" type="xs:string" use="optional"/>
            <xs:attribute name="bookedDateTime" type="xs:string" use="optional"/>
            <xs:attribute name="appointmentSite" type="xs:string" use="optional"/>
            <xs:attribute name="appointmentRoom" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientEntryDate -->
    <xs:complexType name="patientEntryDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="xs:string" use="optional"/>
            <xs:attribute name="patientField" type="xs:string" use="required"/>
            <xs:attribute name="logic" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedCodedEntryDate -->
    <xs:complexType name="derivedCodedEntryDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="codedEntryDateName" type="xs:string" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedFixedDate -->
    <xs:complexType name="derivedFixedDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="fixedDateName" type="xs:string" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedPatientEntryDate -->
    <xs:complexType name="derivedPatientEntryDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="patientEntryDate" type="xs:string" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientAge -->
    <xs:complexType name="patientAgeElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <!-- Not requiredness-checked; real files often omit it (defaults to years). -->
            <xs:attribute name="type" type="xs:string" use="optional"/>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="atDate" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientClinicalSex -->
    <xs:complexType name="patientClinicalSexElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientCareHome -->
    <xs:complexType name="patientCareHomeElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientEthnic -->
    <xs:complexType name="patientEthnicElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientRegistrationType -->
    <xs:complexType name="patientRegistrationTypeElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="patientField" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientRegistrationVariable -->
    <xs:complexType name="patientRegistrationVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="patientRegistrationColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="order" type="patientRegistrationColumnType" use="required"/>
            <xs:attribute name="startDate" type="xs:string" use="optional"/>
            <xs:attribute name="endDate" type="xs:string" use="optional"/>
            <xs:attribute name="registrationType" type="patientRegistrationTypeCategoryListType" use="optional"/>
            <xs:attribute name="prescriptionsDispensedByPractice" type="trueFalseAttributeType" use="optional"/>
            <xs:attribute name="careHomeResident" type="trueFalseAttributeType" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- entryDateFromList -->
    <xs:complexType name="entryDateFromListElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="patientField" type="xs:string" use="optional"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
            <xs:attribute name="codedEntryDateList" type="xs:string" use="required"/>
            <xs:attribute name="logic" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedEntryDateFromList -->
    <xs:complexType name="derivedEntryDateFromListElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="entryDateFromListName" type="xs:string" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedCodedEntryVariable -->
    <xs:complexType name="derivedCodedEntryVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attribute name="codedEntryVariableName" type="xs:string" use="required"/>
                <xs:attribute name="operation" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- codedEntryValueFromList -->
    <xs:complexType name="codedEntryValueFromListElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="xs:string" use="required"/>
            <xs:attribute name="valueList" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- specialCase -->
    <xs:complexType name="specialCaseElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
            <xs:attribute name="codeList" type="xs:string" use="optional"/>
            <xs:attribute name="logic" type="xs:string" use="optional"/>
            <xs:attribute name="codedEntryName" type="xs:string" use="optional"/>
            <xs:attribute name="recordedOn" type="xs:string" use="optional"/>
            <xs:attribute name="encounterTypeCode" type="xs:string" use="optional"/>
            <xs:attribute name="entryWhereParent" type="xs:string" use="optional"/>
            <xs:attribute name="entryWhereChild" type="xs:string" use="optional"/>
            <xs:attribute name="case" type="xs:string" use="optional"/>
            <xs:attribute name="minusDate" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientNamedGp -->
    <xs:complexType name="patientNamedGpElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientRegisteredGpPractice -->
    <xs:complexType name="patientRegisteredGpPracticeElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- patientQuery -->
    <xs:complexType name="patientQueryVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="queryIdentifier" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedBooleanValue -->
    <xs:complexType name="derivedBooleanValueElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- prescriptionVariable -->
    <xs:complexType name="prescriptionVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="prescriptionColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="optional"/>
            <xs:attribute name="order" type="prescriptionColumnType" use="optional"/>
            <xs:attribute name="type" type="prescriptionTypeListType" use="optional"/>
            <xs:attribute name="status" type="prescriptionStatusListType" use="optional"/>
            <xs:attribute name="productCode" type="xs:string" use="optional"/>
            <xs:attribute name="excludeProductCode" type="xs:string" use="optional"/>
            <xs:attribute name="lastIssueFields" type="prescriptionLastIssueColumnListType" use="optional"/>
            <xs:attribute name="lastIssueDate" type="xs:string" use="optional"/>
            <xs:attribute name="endDate" type="xs:string" use="optional"/>
            <xs:attribute name="startDate" type="xs:string" use="optional"/>
            <xs:attribute name="discontinuedDate" type="xs:string" use="optional"/>
            <xs:attribute name="authorisingPractitioner" type="xs:string" use="optional"/>
            <xs:attribute name="authorisingPractitionerIdentifierType" type="xs:string" use="optional"/>
            <xs:attribute name="authorisingPractitionerIdentifierValue" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- prescriptionIssueVariable -->
    <xs:complexType name="prescriptionIssueVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="prescriptionIssueColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="optional"/>
            <xs:attribute name="order" type="prescriptionIssueColumnType" use="optional"/>
            <xs:attribute name="limit" type="xs:positiveInteger" use="optional"/>
            <xs:attribute name="productCode" type="xs:string" use="optional"/>
            <xs:attribute name="excludeProductCode" type="xs:string" use="optional"/>
            <xs:attribute name="issueDate" type="xs:string" use="optional"/>
            <xs:attribute name="issueMethod" type="issueMethodListType" use="optional"/>
            <xs:attribute name="issueStatus" type="issueStatusListType" use="optional"/>
            <xs:attribute name="prescriptionStatus" type="prescriptionStatusListType" use="optional"/>
            <xs:attribute name="prescriptionType" type="prescriptionTypeListType" use="optional"/>
            <xs:attribute name="issuedByPractitioner" type="xs:string" use="optional"/>
            <xs:attribute name="issuedByPractitionerIdentifierType" type="xs:string" use="optional"/>
            <xs:attribute name="issuedByPractitionerIdentifierValue" type="xs:string" use="optional"/>
            <xs:attribute name="quantityValue" type="xs:string" use="optional"/>
            <xs:attribute name="quantityUnitCode" type="xs:string" use="optional"/>
            <xs:attribute name="supplyEndDate" type="xs:string" use="optional"/>
            <xs:attribute name="pharmacyOdsCode" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedPrescriptionIssueDate -->
    <xs:complexType name="derivedPrescriptionIssueDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="prescriptionIssueVariable" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedPrescriptionIssueDescription -->
    <xs:complexType name="derivedPrescriptionIssueDescriptionElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="prescriptionIssueVariable" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedPrescriptionDate -->
    <xs:complexType name="derivedPrescriptionDateElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="prescriptionVariable" type="xs:string" use="required"/>
            <xs:attribute name="dateField" type="prescriptionColumnType" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
            <xs:attribute name="operation" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- clinicalCaseVariable -->
    <xs:complexType name="clinicalCaseVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="clinicalCaseColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="order" type="clinicalCaseColumnType" use="required"/>
            <xs:attribute name="status" type="clinicalCaseStatusListType" use="optional"/>
            <xs:attribute name="type" type="xs:string" use="optional"/>
            <xs:attribute name="commissionedServiceOrganisation" type="xs:string" use="optional"/>
            <xs:attribute name="commissionedServiceServiceIdentifier" type="xs:string" use="optional"/>
            <xs:attribute name="createdDateTime" type="xs:string" use="optional"/>
            <xs:attribute name="closedDateTime" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="clinicalPathwayVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="clinicalPathwayColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="order" type="clinicalPathwayColumnType" use="required"/>
            <xs:attribute name="status" type="clinicalPathwayStatusListType" use="optional"/>
            <xs:attribute name="pathway" type="xs:string" use="optional"/>
            <xs:attribute name="currentStage" type="xs:string" use="optional"/>
            <xs:attribute name="createdDateTime" type="xs:string" use="optional"/>
            <xs:attribute name="removedDateTime" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="clinicalPathwayStageChangeVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="clinicalPathwayStageChangeColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="order" type="clinicalPathwayStageChangeColumnType" use="required"/>
            <xs:attribute name="pathway" type="xs:string" use="optional"/>
            <xs:attribute name="fromStage" type="xs:string" use="optional"/>
            <xs:attribute name="toStage" type="xs:string" use="optional"/>
            <xs:attribute name="changedDateTime" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- codedEntryVariable -->
    <xs:complexType name="codedEntryVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="select" type="codedEntryColumnListType" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="optional"/>
            <xs:attribute name="order" type="codedEntryColumnType" use="optional"/>
            <!-- Optional cap for take="latest"/"earliest": take the top N records per patient. -->
            <xs:attribute name="limit" type="xs:positiveInteger" use="optional"/>
            <xs:attribute name="entityType" type="codedEntryEntityTypeListType" use="optional"/>
            <xs:attribute name="code" type="xs:string" use="optional"/>
            <xs:attribute name="encounterTypeCode" type="xs:string" use="optional"/>
            <xs:attribute name="clinicallyRelevantDate" type="xs:string" use="optional"/>
            <xs:attribute name="endDate" type="xs:string" use="optional"/>
            <xs:attribute name="distinct" type="codedEntryColumnType" use="optional"/>
            <xs:attribute name="status" type="codedEntryStatusListType" use="optional"/>
            <xs:attribute name="value1" type="xs:string" use="optional"/>
            <xs:attribute name="value2" type="xs:string" use="optional"/>
            <xs:attribute name="valueUnitCode" type="xs:string" use="optional"/>
            <xs:attribute name="bodySiteCode" type="xs:string" use="optional"/>
            <xs:attribute name="routeCode" type="xs:string" use="optional"/>
            <xs:attribute name="performedAtLocalOrganisation" type="xs:string" use="optional"/>
            <xs:attribute name="practitionerIdentifierType" type="xs:string" use="optional"/>
            <xs:attribute name="practitionerIdentifierValue" type="xs:string" use="optional"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- codedEntryFromListVariable -->
    <xs:complexType name="codedEntryFromListVariableElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="take" type="takeMethodType" use="required"/>
            <xs:attribute name="codedEntryVariableList" type="xs:string" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- derivedCodedEntryPractitioner -->
    <xs:complexType name="derivedCodedEntryPractitionerElementType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="codedEntryVariable" type="xs:string" use="required"/>
            <xs:attribute name="fetch" type="fetchMethodType" use="required"/>
                <xs:attributeGroup ref="variableDebugAttributeGroup"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- aggregatedEntryDate -->
    <xs:complexType name="aggregatedEntryDateElementType">
        <xs:sequence>
            <xs:element name="if" type="xs:string"/>
            <xs:element name="then" type="xs:string"/>
            <xs:element name="else" type="xs:string"/>
        </xs:sequence>
        <!-- name is read unconditionally with no requiredness check; some real files omit it. -->
        <xs:attribute name="name" type="xs:string" use="optional"/>
        <!-- Present on real files but never read by PatientQueryAggregatedEntryDate. -->
        <xs:attribute name="codeList" type="xs:string" use="optional"/>
        <xs:attributeGroup ref="variableDebugAttributeGroup"/>
    </xs:complexType>

    <!-- conditionalCodedEntryVariable: the SQL form of aggregatedEntryDate, same children; no else reads as NULL -->
    <xs:complexType name="conditionalCodedEntryVariableElementType">
        <xs:sequence>
            <xs:element name="if" type="xs:string"/>
            <xs:element name="then" type="xs:string"/>
            <xs:element name="else" type="xs:string" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attributeGroup ref="variableDebugAttributeGroup"/>
    </xs:complexType>

    <!-- ===== variables wrapper: every tag optional, repeatable, order-agnostic ===== -->

    <xs:complexType name="variablesElementType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="aggregatedEntryDate" type="aggregatedEntryDateElementType"/>
            <xs:element name="appointmentVariable" type="appointmentVariableElementType"/>
            <xs:element name="clinicalCaseVariable" type="clinicalCaseVariableElementType"/>
            <xs:element name="clinicalPathwayVariable" type="clinicalPathwayVariableElementType"/>
            <xs:element name="clinicalPathwayStageChangeVariable" type="clinicalPathwayStageChangeVariableElementType"/>
            <xs:element name="codedEntryDate" type="codedEntryDateElementType"/>
            <xs:element name="codedEntryFromListVariable" type="codedEntryFromListVariableElementType"/>
            <xs:element name="codedEntryValue" type="codedEntryValueElementType"/>
            <xs:element name="codedEntryValueFromList" type="codedEntryValueFromListElementType"/>
            <xs:element name="codedEntryVariable" type="codedEntryVariableElementType"/>
            <xs:element name="conditionalCodedEntryVariable" type="conditionalCodedEntryVariableElementType"/>
            <xs:element name="derivedBooleanValue" type="derivedBooleanValueElementType"/>
            <xs:element name="derivedCodedEntryDate" type="derivedCodedEntryDateElementType"/>
            <xs:element name="derivedCodedEntryPractitioner" type="derivedCodedEntryPractitionerElementType"/>
            <xs:element name="derivedCodedEntryVariable" type="derivedCodedEntryVariableElementType"/>
            <xs:element name="derivedEntryDateFromList" type="derivedEntryDateFromListElementType"/>
            <xs:element name="derivedFixedDate" type="derivedFixedDateElementType"/>
            <xs:element name="derivedPatientEntryDate" type="derivedPatientEntryDateElementType"/>
            <xs:element name="derivedPrescriptionIssueDate" type="derivedPrescriptionIssueDateElementType"/>
            <xs:element name="derivedPrescriptionIssueDescription" type="derivedPrescriptionIssueDescriptionElementType"/>
            <xs:element name="derivedPrescriptionDate" type="derivedPrescriptionDateElementType"/>
            <xs:element name="entryDateFromList" type="entryDateFromListElementType"/>
            <xs:element name="fixedDate" type="fixedDateElementType"/>
            <xs:element name="patientAge" type="patientAgeElementType"/>
            <xs:element name="patientCareHome" type="patientCareHomeElementType"/>
            <xs:element name="patientClinicalSex" type="patientClinicalSexElementType"/>
            <xs:element name="patientEntryDate" type="patientEntryDateElementType"/>
            <xs:element name="patientEthnic" type="patientEthnicElementType"/>
            <xs:element name="patientNamedGp" type="patientNamedGpElementType"/>
            <xs:element name="patientQuery" type="patientQueryVariableElementType"/>
            <xs:element name="patientRegisteredGpPractice" type="patientRegisteredGpPracticeElementType"/>
            <xs:element name="patientRegisteredGpPracticeName" type="patientRegisteredGpPracticeElementType"/>
            <xs:element name="patientRegistrationType" type="patientRegistrationTypeElementType"/>
            <xs:element name="patientRegistrationVariable" type="patientRegistrationVariableElementType"/>
            <xs:element name="prescriptionIssueVariable" type="prescriptionIssueVariableElementType"/>
            <xs:element name="prescriptionVariable" type="prescriptionVariableElementType"/>
            <xs:element name="specialCase" type="specialCaseElementType"/>
        </xs:choice>
    </xs:complexType>
    <!-- ===== patientQuery root =====

        patientQueryContentType is the reusable content model (no attributes), so a
        caller (e.g. future-action-rule.xsd) can extend it with its own attribute
        constraints. patientQueryElementType adds the `focus` attribute the engine
        itself reads (PatientQueryFocus; optional, v2 report-embedded queries omit it
        and default to PATIENTS).

        Strict about vocabulary, deliberately loose about shape: matches the existing
        precedent in shipped-report.xsd. Required-ness of individual elements (e.g.
        <variables>/<rules>/<population>/<name>/<description> for a full patient
        query) is enforced by PatientQueryDefinition::validateXmlElementsRequirements()
        at runtime, not by this schema (encoding it here would mean policing element
        ordering too, which the corpus does not guarantee and which is not this
        schema's job).

        Out of scope: <logic>/<returnValueFieldName> (PARTIAL_REPORT_PATIENT_QUERY
        report-embedded fragments) stay in shipped-report.xsd/custom-report schema
        territory; this schema models the full/standalone patient-query grammar used
        by on-disk queries/ files and future-action-rule.xsd's inline target query.
    -->

    <xs:complexType name="patientQueryContentType">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="name" type="xs:string"/>
            <xs:element name="description" type="xs:string"/>
            <xs:element name="responsibleOrganisation" type="xs:string"/>
            <xs:element name="externalIdentifier" type="xs:string"/>
            <xs:element name="authors" type="authorsElementType"/>
            <xs:element name="population" type="populationElementType"/>
            <xs:element name="definedPatientIds" type="xs:string"/>
            <xs:element name="codeLists" type="codeListsElementType"/>
            <xs:element name="variables" type="variablesElementType"/>
            <xs:element name="rules" type="rulesElementType"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="patientQueryElementType">
        <xs:complexContent>
            <xs:extension base="patientQueryContentType">
                <xs:attribute name="focus" use="optional">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="patients"/>
                            <xs:enumeration value="care_records"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:element name="patientQuery" type="patientQueryElementType"/>
</xs:schema>
