<?xml version="1.0" encoding="UTF-8"?>
<!--  ==========================================================================
	This is the schema definition for a "General Invoice Response" process encoded
	in XML. The generalInvoiceResponse always constitutes an answer to a received 
	"General Invoice Request". By design there can be [0,n] such responses but 
	solely to the topmost object reference given given in the request. If there is no 
	response sent then the request is considered implicitly as accepted.

	The response is sent via 1 or more intermediates to the final recipient, who 
	is the initial sender of the request by definition. The chain of intermediates 
	from the request is reversed for the response.

	This XML project is normative for the electronic invoice/reminder process in Switzerland.

	Permission to use, and distribute this schema and its accompanying documentation 
	for any purpose and without fee is hereby granted in perpetuity, provided that 
	the above copyright notice and this paragraph appear in all copies and this schema 
	file is not altered.
	The copyright holders make no representation about the suitability of the schema
	for any purpose.

	It is provided "as is" without expressed or implied warranty.

	Copyright (c) 2016 Forum Datenaustausch, 
	All Rights Reserved.
  ==========================================================================  -->
<schema targetNamespace="http://www.forum-datenaustausch.ch/invoice" 
	xmlns="http://www.w3.org/2001/XMLSchema" 
	xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
	xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
	xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" 
	elementFormDefault="qualified" attributeFormDefault="unqualified">

<!-- =======================================================================
	import fo encryption and signature schema definitions as local files
	in fact MSXML4.0 has a problem with network loaded schema definitions
	and there is a significant time impact of always loading these schemas 
	from W3C.
	
	W3C locations:
	   	http://www.w3c.org/TR/xmlenc-core/xenc-schema.xsd
  		http://www.w3c.org/TR/xmldsig-core/xmldsig-core-schema.xsd
 ======================================================================= -->
   <import namespace="http://www.w3.org/2000/09/xmldsig#" 
   		   schemaLocation="xmldsig-core-schema.xsd"/>
   <import namespace="http://www.w3.org/2001/04/xmlenc#" 
   		   schemaLocation="xenc-schema.xsd"/>

<!-- =======================================================================
	General data type definitions
 ======================================================================= -->
	<simpleType name="stringType1_3">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="3"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_9">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="9"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_15">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="15"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_25">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="25"/>
		</restriction>
	</simpleType>

	<simpleType name="stringType1_35">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="35"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_50">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="50"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_70">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="70"/>
		</restriction>
	</simpleType>

	<simpleType name="stringType1_255">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="255"/>
		</restriction>
	</simpleType>

	<simpleType name="stringType1_350">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="350"/>
		</restriction>
	</simpleType>
	
	<simpleType name="stringType1_700">
		<restriction base="string">
			<minLength value="1"/>
			<maxLength value="700"/>
		</restriction>
	</simpleType>
	
	<!--  GLN and SSN numbers -->
	<simpleType name="ssnPartyType">
		<restriction base="string">
			<pattern value="[0-9]{4,10}|[1-9][0-9]{10}|756[0-9]{10}|438[0-9]{10}"/>
		</restriction>
	</simpleType>

	<simpleType name="eanPartyType">
		<restriction base="string">
			<pattern value="([0-9]{13})"/>
		</restriction>
	</simpleType>

<!-- =======================================================================
	address related definitions for persons and companies
 ======================================================================= -->
	<complexType name="personType">
		<sequence>
			<element name="familyname" type="invoice:stringType1_35"/>
			<element name="givenname" type="invoice:stringType1_35"/>
			<element name="subaddressing" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="postal" type="invoice:postalAddressType"/>
			<element name="telecom" type="invoice:telecomAddressType" minOccurs="0"/>
			<element name="online" type="invoice:onlineAddressType" minOccurs="0"/>
		</sequence>
		<attribute name="salutation" type="invoice:stringType1_35"/>
		<attribute name="title" type="invoice:stringType1_35"/>
	</complexType>
	
	<complexType name="companyType">
		<sequence>
			<element name="companyname" type="invoice:stringType1_35"/>
			<element name="department" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="subaddressing" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="postal" type="invoice:postalAddressType"/>
			<element name="telecom" type="invoice:telecomAddressType" minOccurs="0"/>
			<element name="online" type="invoice:onlineAddressType" minOccurs="0"/>
		</sequence>
	</complexType>
	
	<complexType name="postalAddressType">
		<sequence>
			<element name="pobox" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="street" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="zip" type="invoice:zipType"/>
			<element name="city" type="invoice:stringType1_35"/>
		</sequence>
	</complexType>

	<complexType name="zipType">
		<simpleContent>
			<extension base="invoice:stringType1_9">
				<attribute name="statecode" type="invoice:stringType1_9"/>
				<attribute name="countrycode" type="invoice:stringType1_3" default="CH"/>
			</extension>
		</simpleContent>
	</complexType>

	<complexType name="telecomAddressType">
		<sequence>
			<element name="phone" type="invoice:stringType1_25" maxOccurs="3"/>
			<element name="fax" type="invoice:stringType1_25" minOccurs="0" maxOccurs="3"/>
		</sequence>
	</complexType>

	<complexType name="onlineAddressType">
		<sequence>
			<element name="email" maxOccurs="3">
				<simpleType>
					<restriction base="invoice:stringType1_70">
						<pattern value=".+@.+"/>
					</restriction>
				</simpleType>
			</element>
			<element name="url" minOccurs="0" maxOccurs="3">
				<simpleType>
					<restriction base="anyURI">
						<minLength value="1"/>
						<maxLength value="100"/>
					</restriction>
				</simpleType>
			</element>
		</sequence>
	</complexType>

	<complexType name="patientAddressType">
		<sequence>
			<element name="person" type="invoice:personType"/>
		</sequence>
		<attribute name="gender" use="required">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="male"/>
					<enumeration value="female"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="birthdate" type="dateTime" use="required"/>
		<attribute name="ssn" type="invoice:ssnPartyType"/>
	</complexType>

	<complexType name="contactAddressType">
		<choice>
			<element name="person" type="invoice:personType"/>
			<sequence>
				<element name="company" type="invoice:companyType"/>
				<element name="employee" type="invoice:employeeType" minOccurs="0"/>
			</sequence>
		</choice>
		<attribute name="ean_party" type="invoice:eanPartyType"/>
	</complexType>

	<complexType name="employeeType">
		<sequence>
			<element name="familyname" type="invoice:stringType1_35"/>
			<element name="givenname" type="invoice:stringType1_35" minOccurs="0"/>
			<element name="telecom" type="invoice:telecomAddressType" minOccurs="0"/>
			<element name="online" type="invoice:onlineAddressType" minOccurs="0"/>
		</sequence>
		<attribute name="salutation" type="invoice:stringType1_35"/>
		<attribute name="title" type="invoice:stringType1_35"/>
	</complexType>

	<complexType name="esrAddressType">
		<choice>
			<element name="person" type="invoice:personType"/>
			<element name="company" type="invoice:companyType"/>
		</choice>
	</complexType>

<!-- =======================================================================
	reimbursement: data type definitions regarding a reimbursement process

	Note, that in such a process there are 2 address roles (debitor, creditor) 
	involved plus an esr payment part (that optionally includes a bank 
	address as 3rd address role).

	The debitor is the mirrored biller from the request and the creditor is 
	(most often) the mirrored debitor from the request since the flow of money is reversed.
 ======================================================================= -->
	<complexType name="reimbursementType">
		<sequence>
			<element name="debitor" type="invoice:esrAddressType"/>
			<element name="balance" type="invoice:balanceType"/>
			<choice>
				<element name="esr9" type="invoice:esr9Type"/>
				<element name="esrRed" type="invoice:esrRedType"/>
				<element name="esrQR" type="invoice:esrQRType"/>
			</choice>
		</sequence>
	</complexType>

	<complexType name="balanceType">
		<attribute name="currency" type="string" fixed="CHF"/>
		<attribute name="amount_due" type="double" use="required"/>
		<attribute name="vat" type="double" use="required"/>
		<attribute name="vat_number" type="invoice:stringType1_15"/>
		<attribute name="payment_period" type="duration"/>
	</complexType>

	<complexType name="esr9Type">
		<sequence>
			<element name="bank" type="invoice:esrAddressType" minOccurs="0"/>
			<element name="creditor" type="invoice:esrAddressType"/>
		</sequence>
		<attribute name="type" default="16or27">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="16or27plus"/>
					<enumeration value="16or27"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="reference_number" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="([0-9] [0-9]{5} [0-9]{5} [0-9]{5}|[0-9]{2} [0-9]{5} [0-9]{5} [0-9]{5} [0-9]{5} [0-9]{5})"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="participant_number" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="01-[1-9][0-9]{0,5}-[0-9]"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="coding_line" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="(01[0-9]{11}&gt;[0-9]{16}\+ [0-9]{9}&gt;|042&gt;[0-9]{16}\+ [0-9]{9}&gt;|01[0-9]{11}&gt;[0-9]{27}\+ [0-9]{9}&gt;|042&gt;[0-9]{27}\+ [0-9]{9}&gt;)"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>

	<complexType name="esrRedType">
		<sequence>
			<element name="bank" type="invoice:esrAddressType" minOccurs="0"/>
			<element name="creditor" type="invoice:esrAddressType"/>
			<element name="payment_reason" type="invoice:stringType1_35" minOccurs="0" maxOccurs="4"/>
		</sequence>
		<attribute name="payment_to" use="required">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="postal_account"/>
					<enumeration value="bank_account"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="post_account" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="[0-9]{2}-[0-9]{1,6}-[0-9]"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="iban">
			<simpleType>
				<restriction base="string">
					<pattern value="(LI|CH)[0-9]{7}[0-9A-Z]{12}"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="reference_number">
			<simpleType>
				<restriction base="string">
					<pattern value="[0-9A-Z]{10}"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="coding_line1" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="([0-9]{27}\+ 07[0-9]{7}&gt;|[0-9]{9}&gt;)"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="coding_line2" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="[0-9]{9}&gt;"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>

	<complexType name="esrQRType">
		<sequence>
			<element name="bank" type="invoice:esrAddressType"/>
			<element name="creditor" type="invoice:esrAddressType"/>
			<element name="payment_reason" type="invoice:stringType1_35" minOccurs="0" maxOccurs="4"/>
		</sequence>
		<attribute name="type" default="esrQR">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="esrQRplus"/>
					<enumeration value="esrQR"/>
					<enumeration value="redPayinSlipQR"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="iban" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="(LI|CH)[0-9]{7}[0-9A-Z]{12}"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="reference_number">
			<simpleType>
				<restriction base="string">
					<pattern value="[0-9]{27}|RF[0-9]{2}[0-9A-Z]{1,21}"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="customer_note" type="invoice:stringType1_70"/>
	</complexType>

<!-- =======================================================================
	data type definitions for the 3 fundamental response status types
	pending | accepted | rejected 
 ======================================================================= -->
	<complexType name="statusType">
		<attribute name="status_in" use="required">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="unknown"/>
					<enumeration value="ambiguous"/>
					<enumeration value="received"/>
					<enumeration value="frozen"/>
					<enumeration value="processed"/>
					<enumeration value="granted"/>
					<enumeration value="canceled"/>
					<enumeration value="claimed"/>
					<enumeration value="reimbursed"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="status_out" use="required">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="unknown"/>
					<enumeration value="ambiguous"/>
					<enumeration value="received"/>
					<enumeration value="frozen"/>
					<enumeration value="processed"/>
					<enumeration value="granted"/>
					<enumeration value="canceled"/>
					<enumeration value="claimed"/>
					<enumeration value="reimbursed"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>

	<complexType name="pendingType">
		<complexContent>
			<extension base="invoice:statusType">
			<sequence>
				<element name="explanation" type="invoice:stringType1_700" minOccurs="0"/>
				<element name="message" type="invoice:notificationType" minOccurs="0" maxOccurs="unbounded"/>
			</sequence>
			</extension>
		</complexContent>
	</complexType>

	<complexType name="acceptedType">
		<complexContent>
			<extension base="invoice:statusType">
			<sequence>
				<element name="explanation" type="invoice:stringType1_700" minOccurs="0"/>
				<element name="reimbursement" type="invoice:reimbursementType" minOccurs="0"/>
			</sequence>
			</extension>
		</complexContent>
	</complexType>

	<complexType name="rejectedType">
		<complexContent>
			<extension base="invoice:statusType">
			<sequence>
				<element name="explanation" type="invoice:stringType1_700" minOccurs="0"/>
				<element name="error" type="invoice:errorType" minOccurs="0" maxOccurs="unbounded"/>
			</sequence>
			</extension>
		</complexContent>
	</complexType>

	<complexType name="notificationType">
		<attribute name="code" type="invoice:stringType1_35" use="required"/>
		<attribute name="text" type="invoice:stringType1_350" use="required"/>
	</complexType>

	<complexType name="errorType">
		<complexContent>
			<extension base="invoice:notificationType">
				<attribute name="error_value" type="invoice:stringType1_350"/>
				<attribute name="valid_value" type="invoice:stringType1_350"/>
				<attribute name="record_id" type="positiveInteger"/>
			</extension>
		</complexContent>
	</complexType>

	<complexType name="partyType">
		<attribute name="ean_party" type="invoice:eanPartyType" use="required"/>
	</complexType>

	<complexType name="processingType">
		<sequence>
			<element name="transport" type="invoice:transportType"/>
		</sequence>
	</complexType>
	
	<complexType name="transportType">
		<sequence>
			<element name="via" maxOccurs="unbounded">
				<complexType>
					<attribute name="via" type="invoice:eanPartyType" use="required"/>
					<attribute name="sequence_id" type="unsignedShort" use="required"/>
				</complexType>
			</element>
		</sequence>
		<attribute name="from" type="invoice:eanPartyType" use="required"/>
		<attribute name="to" type="invoice:eanPartyType" use="required"/>
	</complexType>

<!-- =======================================================================
	prolog related definitions with the chain of involved/used software
 ======================================================================= -->
	<complexType name="prologType">
		<sequence>
			<element name="package" type="invoice:softwareType" minOccurs="0"/>
			<element name="generator" type="invoice:softwareType"/>
		</sequence>
	</complexType>

	<complexType name="softwareType">
		<sequence>
			<element name="depends_on" type="invoice:dependsOnType" minOccurs="0" maxOccurs="unbounded"/>
		</sequence>
		<attribute name="name" type="invoice:stringType1_50" use="required"/>
		<attribute name="copyright" type="invoice:stringType1_50"/>
		<attribute name="description" type="invoice:stringType1_50"/>
		<attribute name="version" type="unsignedInt"/>
		<attribute name="id" type="unsignedInt" default="0"/>
	</complexType>

	<complexType name="dependsOnType">
		<attribute name="name" type="invoice:stringType1_50" use="required"/>
		<attribute name="copyright" type="invoice:stringType1_50"/>
		<attribute name="description" type="invoice:stringType1_50"/>
		<attribute name="version" type="unsignedInt"/>
		<attribute name="id" type="unsignedInt" default="0"/>
	</complexType>

	<complexType name="creditType">
		<attribute name="request_timestamp" use="required">
			<simpleType>
				<restriction base="unsignedLong">
					<minInclusive value="1420066800"/>
					<maxInclusive value="1924902000"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="request_date" type="dateTime" use="required"/>
		<attribute name="request_id" type="invoice:stringType1_35" use="required"/>
	</complexType>
	
	<complexType name="invoiceType">
		<attribute name="request_timestamp" use="required">
			<simpleType>
				<restriction base="unsignedLong">
					<minInclusive value="1420066800"/>
					<maxInclusive value="1924902000"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="request_date" type="dateTime" use="required"/>
		<attribute name="request_id" type="invoice:stringType1_35" use="required"/>
	</complexType>
	
	<complexType name="reminderType">
		<attribute name="request_timestamp" use="required">
			<simpleType>
				<restriction base="unsignedLong">
					<minInclusive value="1420066800"/>
					<maxInclusive value="1924902000"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="request_date" type="dateTime" use="required"/>
		<attribute name="request_id" type="invoice:stringType1_35" use="required"/>
		<attribute name="reminder_level" default="1">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="1"/>
					<enumeration value="2"/>
					<enumeration value="3"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>

<!-- =======================================================================
	APID/ACID treatment information definitions
 ======================================================================= -->
	<complexType name="treatmentType">
		<attribute name="apid" type="invoice:stringType1_35"/>
		<attribute name="acid" type="invoice:stringType1_35"/>
	</complexType>

	<complexType name="documentsType">
		<sequence>
			<element name="document" type="invoice:documentType" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
		<attribute name="number" type="unsignedLong" use="required"/>
	</complexType>

	<complexType name="documentType">
		<sequence>
			<element name="base64" type="base64Binary"/>
		</sequence>
		<attribute name="title" type="invoice:stringType1_35" use="required"/>
		<attribute name="filename" type="invoice:stringType1_255" use="required"/>
		<attribute name="mimeType" use="required">
			<simpleType>
				<restriction base="string">
					<pattern value="(application/pdf|image/jpeg|image/png)"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>

<!-- =======================================================================
	body definition with all sensitive data regarding data privacy
	This element and subelements therein can be encrypted 
 ======================================================================= -->
	<complexType name="bodyType">
		<sequence>
			<element name="prolog" type="invoice:prologType"/>
			<element name="biller" type="invoice:partyType"/>
			<element name="debitor" type="invoice:partyType"/>
			<element name="provider" type="invoice:partyType"/>
			<element name="insurance" type="invoice:partyType"/>
			<element name="patient" type="invoice:patientAddressType"/>
			<element name="treatment" type="invoice:treatmentType" minOccurs="0"/>

			<element name="contact" type="invoice:contactAddressType"/>
			<choice>
				<element name="pending" type="invoice:pendingType"/>
				<element name="accepted" type="invoice:acceptedType"/>
				<element name="rejected" type="invoice:rejectedType"/>
			</choice>
			<element name="documents" type="invoice:documentsType" minOccurs="0"/>
		</sequence>
	</complexType>

<!-- =======================================================================
	payload definitions with the mirrored chain of object references (credit, 
	invoice, and reminder) plus the sensitive data regarding data privacy
	This element and subelements therein can be signed
 ======================================================================= -->
	<complexType name="payloadType">
		<sequence>
			<element name="credit" type="invoice:creditType" minOccurs="0"/>
			<element name="invoice" type="invoice:invoiceType"/>
			<element name="reminder" type="invoice:reminderType" minOccurs="0"/>
			<choice>
				<element name="body" type="invoice:bodyType"/>
				<element ref="xenc:EncryptedData"/>
			</choice>
		</sequence>
		<attribute name="type" default="invoice">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="invoice"/>
					<enumeration value="reminder"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="storno" type="boolean" default="false"/>
		<attribute name="copy" type="boolean" default="false"/>
		<attribute name="response_timestamp" use="required">
			<simpleType>
				<restriction base="unsignedLong">
					<minInclusive value="1420066800"/>
					<maxInclusive value="1924902000"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>
	
<!-- =======================================================================
     =======================================================================
	Here starts the response with its root element
 =======================================================================
 ======================================================================= -->
	<element name="response" type="invoice:responseType"/>


	<complexType name="responseType">
		<sequence>
			<element name="processing" type="invoice:processingType"/>
			<element name="payload" type="invoice:payloadType"/>
			<element ref="ds:Signature" minOccurs="0"/>
		</sequence>

		<attribute name="language" use="required">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="de"/>
					<enumeration value="fr"/>
					<enumeration value="it"/>
				</restriction>
			</simpleType>
		</attribute>
		<attribute name="modus" default="production">
			<simpleType>
				<restriction base="NMTOKEN">
					<enumeration value="production"/>
					<enumeration value="test"/>
				</restriction>
			</simpleType>
		</attribute>
	</complexType>
</schema>
