<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="TWSExchange">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="head" maxOccurs="1"/>
        <xs:element ref="goods" maxOccurs="1"/>
        <xs:element ref="process" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- Element HEAD -->
  <xs:element name="head">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="sender"/>
        <xs:element ref="receiver"/>
        <xs:element ref="message_type"/>
        <xs:element ref="number_of_packages" minOccurs="0"/>
        <xs:element ref="reference"/>
        <xs:element ref="export_origin" minOccurs="0"/>
        <xs:element ref="erp_reference"/>
        <xs:element ref="created_date_time"/>
        <xs:element ref="vehicle_id" minOccurs="0"/>
        <xs:element ref="vehicle_origin" minOccurs="0"/>
        <xs:element ref="vehicle_phone" minOccurs="0"/>
        <xs:element ref="customs_office" minOccurs="0"/>
        <xs:element ref="delivery_conditions"/>
        <xs:element ref="goods_locale_code" minOccurs="0"/>
        <xs:element ref="goods_locale_type" minOccurs="0"/>
        <xs:element ref="guarantee" minOccurs="0"/>
        <xs:element ref="no_of_packages" minOccurs="0"/>
        <xs:element ref="packages_type" minOccurs="0"/>
        <xs:element ref="address_f2"/>
        <xs:element ref="address_f8"/>
        <xs:element ref="address_f14"/>
        <xs:element ref="procedure_code" minOccurs="0"/>
        <xs:element ref="invoice_reference"/>
        <xs:element ref="invoice_total" minOccurs="0"/>
        <xs:element ref="invoice_date" minOccurs="0"/>
        <xs:element ref="create_declaration" minOccurs="0"/>
        <xs:element ref="invoice_currency" minOccurs="0"/>
        <xs:element ref="export_destination" minOccurs="0"/>
        <xs:element ref="destination" minOccurs="0"/>
        <xs:element ref="total_quantity" minOccurs="0"/>
        <xs:element ref="incomming_goods_number" minOccurs="0"/>
        <xs:element ref="incoming_goods_number" minOccurs="0"/>
        <xs:element ref="vat_number" minOccurs="0"/>
        <xs:element ref="eori" minOccurs="0"/>
        <xs:element ref="head_note" minOccurs="0"/>
        <xs:element ref="order_type" minOccurs="0"/>
        <xs:element ref="order_ref_1" minOccurs="0"/>
        <xs:element ref="order_ref_2" minOccurs="0"/>
        <xs:element ref="transport_type_at_border" minOccurs="0"/>
        <xs:element ref="transport_type_domestic" minOccurs="0"/>
        <xs:element ref="container" minOccurs="0"/>
        <xs:element ref="goods_location" minOccurs="0"/>
        <xs:element ref="previousdocuments" minOccurs="0"/>
        <xs:element ref="authorisation_holder" minOccurs="0"/>
        <xs:element ref="test_flag" minOccurs="0"/>
        <xs:element ref="estimated_time_of_arrival" minOccurs="0"/>
        <xs:element ref="declare_vat" minOccurs="0"/>
        <xs:element ref="nature_of_transaction" minOccurs="0"/>
        <xs:element ref="integration_data" minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="sender" type="xs:string"/>
  <xs:element name="receiver" type="xs:string"/>
  <xs:element name="message_type" type="xs:string"/>
  <xs:element name="number_of_packages" type="xs:integer"/>
  <xs:element name="reference" type="xs:string"/>
  <xs:element name="export_origin" type="validatecountrycode"/>
  <xs:element name="erp_reference" type="xs:string"/>
  <xs:element name="created_date_time" type="xs:dateTime"/>
  <xs:element name="vehicle_id" type="xs:string"/>
  <xs:element name="vehicle_origin" type="xs:string"/>
  <xs:element name="vehicle_phone" type="xs:string"/>
  <xs:element name="customs_office" type="xs:string"/>
  <xs:element name="delivery_conditions" type="xs:string"/>
  <xs:element name="goods_locale_code" type="xs:string"/>
  <xs:element name="goods_locale_type" type="xs:string"/>
  <xs:element name="guarantee" type="xs:string"/>
  <xs:element name="no_of_packages" type="xs:integer"/>
  <xs:element name="packages_type" type="xs:string"/>
  <xs:element name="address_f2" type="xs:string"/>
  <xs:element name="address_f8" type="xs:string"/>
  <xs:element name="address_f14" type="xs:string"/>
  <xs:element name="invoice_reference" type="xs:string"/>
  <xs:element name="invoice_total" type="positivedecimalzero"/>
  <xs:element name="invoice_date" type="xs:dateTime"/>
  <xs:element name="create_declaration" type="xs:boolean"/>
  <xs:element name="invoice_currency" type="validatecurrency"/>
  <xs:element name="destination" type="xs:string"/>
  <xs:element name="export_destination" type="validatecountrycode"/>
  <xs:element name="total_quantity" type="xs:string"/>
  <xs:element name="incomming_goods_number" type="xs:string"/>
  <xs:element name="incoming_goods_number" type="xs:string"/>
  <xs:element name="vat_number" type="xs:string"/>
  <xs:element name="eori" type="xs:string"/>
  <xs:element name="head_note" type="xs:string"/>
  <xs:element name="order_type" type="xs:string"/>
  <xs:element name="order_ref_1" type="xs:string"/>
  <xs:element name="order_ref_2" type="xs:string"/>
  <xs:element name="transport_type_at_border" type="xs:string"/>
  <xs:element name="transport_type_domestic" type="xs:string"/>
  <xs:element name="declare_vat" type="xs:boolean"/>
  <xs:element name="nature_of_transaction" type="xs:string"/>

  <!-- Element GOODS -->
  <xs:element name="goods">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element ref="row"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="row">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="description"/>
        <xs:element ref="goods_id_se" minOccurs="0"/>
        <xs:element ref="goods_id_no" minOccurs="0"/>
        <xs:element ref="goods_origin"/>
        <xs:element ref="gross_weight" minOccurs="0"/>
        <xs:element ref="benefit" minOccurs="0"/>
        <xs:element ref="net_weight" minOccurs="0"/>
        <xs:element ref="additional_unit_se" minOccurs="0"/>
        <xs:element ref="additional_unit_no" minOccurs="0"/>
        <xs:element ref="price_currency" minOccurs="0"/>
        <xs:element ref="price" minOccurs="0"/>
        <xs:element ref="freight_cost" minOccurs="0"/>
        <xs:element ref="certificate_type" minOccurs="0"/>
        <xs:element ref="certificate_reference" minOccurs="0"/>
        <xs:element ref="certificate_date_time" minOccurs="0"/>
        <xs:element ref="certificate_end_date_time" minOccurs="0"/>
        <xs:element ref="certificate_quantity" minOccurs="0"/>
        <xs:element ref="textile_licence_number" minOccurs="0"/>
        <xs:element ref="textile_licence_amount" minOccurs="0"/>
        <xs:element ref="invoice_declaration" minOccurs="0"/>
        <xs:element ref="invocie_declaration" minOccurs="0"/>
        <xs:element ref="exchange_rate" minOccurs="0"/>
        <xs:element ref="vat_code" minOccurs="0"/>
        <xs:element ref="taxes" minOccurs="0"/>
        <xs:element ref="internal_reference"/>
        <xs:element ref="alarm" minOccurs="0"/>
        <xs:element ref="additional_information" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="no_of_packages" minOccurs="0"/>
        <xs:element ref="packages_type" minOccurs="0"/>
        <xs:element ref="packaging" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="additional_reference" minOccurs="0"/>
        <xs:element ref="eu_shipping" minOccurs="0"/>
        <xs:element ref="non_eu_shipping" minOccurs="0"/>
        <xs:element ref="eu_goods" minOccurs="0"/>
        <xs:element ref="quantity" minOccurs="0"/>
        <xs:element ref="destination" minOccurs="0"/>
        <xs:element ref="freight_currency" minOccurs="0"/>
        <xs:element ref="delivery_conditions" minOccurs="0"/>
        <xs:element ref="additional_reference_2" minOccurs="0"/>
        <xs:element ref="additional_status" minOccurs="0"/>
        <xs:element ref="export_origin" minOccurs="0"/>
        <xs:element ref="procedure_code" minOccurs="0"/>
        <xs:element ref="exchange_currency" minOccurs="0"/>
        <xs:element ref="freight_exchange_rate" minOccurs="0"/>
        <xs:element ref="afs_status" minOccurs="0"/>
        <xs:element ref="row_note" minOccurs="0"/>
        <xs:element ref="additional_reference_3" minOccurs="0"/>
        <xs:element ref="additional_codes" minOccurs="0"/>
        <xs:element ref="document_codes" minOccurs="0"/>
        <xs:element ref="previousdocuments" minOccurs="0"/>
        <xs:element ref="container" minOccurs="0"/>
        <xs:element ref="goodsstatuscode" minOccurs="0"/>
        <xs:element ref="additionaldocuments" minOccurs="0"/>
        <xs:element ref="goods_location" minOccurs="0" maxOccurs="1"/>
        <xs:element ref="integration_data" minOccurs="0" maxOccurs="1"/>
        <xs:element ref="tariff_units" minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="id" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string"/>
  <xs:element name="goods_id_se" type="validategoodsid"/>
  <xs:element name="goods_id_no" type="validategoodsid"/>
  <xs:element name="goods_origin" type="validatecountrycode"/>
  <xs:element name="gross_weight" type="positivedecimalzero"/>
  <xs:element name="benefit" type="xs:string"/>
  <xs:element name="net_weight" type="positivedecimalzero"/>
  <xs:element name="additional_unit_se" type="xs:string"/>
  <xs:element name="additional_unit_no" type="xs:string"/>
  <xs:element name="price_currency" type="validatecurrency"/>
  <xs:element name="price" type="positivedecimalzero"/>
  <xs:element name="freight_cost" type="positivedecimalzero"/>
  <xs:element name="certificate_type" type="xs:string"/>
  <xs:element name="certificate_reference" type="xs:string"/>
  <xs:element name="certificate_date_time" type="xs:dateTime"/>
  <xs:element name="certificate_end_date_time" type="xs:dateTime"/>
  <xs:element name="certificate_quantity" type="xs:integer"/>
  <xs:element name="textile_licence_number" type="xs:string"/>
  <xs:element name="textile_licence_amount" type="positivedecimalzero"/>
  <xs:element name="invoice_declaration" type="xs:string"/>
  <xs:element name="invocie_declaration" type="xs:string"/>
  <xs:element name="exchange_rate" type="positivedecimalzero"/>
  <xs:element name="vat_code" type="positivedecimalzero"/>
  <xs:element name="taxes">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="tax"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tax">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="tax_code"/>
        <xs:element ref="tax_base"/>
        <xs:element ref="tax_amount"/>
        <xs:element ref="tax_unit"/>
        <xs:element ref="tax_rate" minOccurs="0"/>
        <xs:element ref="tax_sequence" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tax_code" type="xs:string"/>
  <xs:element name="tax_base" type="positivedecimalzero"/>
  <xs:element name="tax_amount" type="positivedecimalzero"/>
  <xs:element name="tax_unit" type="xs:string"/>
  <xs:element name="tax_rate" type="positivedecimalzero"/>
  <xs:element name="tax_sequence" type="xs:positiveInteger"/>
  <xs:element name="internal_reference" type="xs:string"/>
  <xs:element name="alarm">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="event"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="event" type="xs:string"/>
  <xs:element name="additional_information">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="type" minOccurs="0"/>
        <xs:element ref="code" minOccurs="0"/>
        <xs:element ref="text" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="type" type="xs:string"/>
  <xs:element name="code" type="xs:string"/>
  <xs:element name="text" type="xs:string"/>
  <xs:element name="additional_reference" type="xs:string"/>
  <xs:element name="eu_shipping" type="positivedecimalzero"/>
  <xs:element name="non_eu_shipping" type="positivedecimalzero"/>
  <xs:element name="eu_goods" type="xs:boolean"/>
  <xs:element name="quantity" type="xs:positiveInteger"/>
  <xs:element name="freight_currency" type="validatecurrency"/>
  <xs:element name="additional_reference_2" type="xs:string"/>
  <xs:element name="additional_status" type="xs:string"/>
  <xs:element name="exchange_currency" type="validatecurrency"/>
  <xs:element name="freight_exchange_rate" type="positivedecimalzero"/>
  <xs:element name="afs_status" type="xs:boolean"/>
  <xs:element name="row_note" type="xs:string"/>
  <xs:element name="additional_reference_3" type="xs:string"/>
  <xs:element name="additional_codes">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="addcode1"/>
        <xs:element ref="addcode2"/>
        <xs:element ref="addcode3"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="addcode1" type="xs:string"/>
  <xs:element name="addcode2" type="xs:string"/>
  <xs:element name="addcode3" type="xs:string"/>
  <xs:element name="document_codes">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="doccode" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="doccode" type="xs:string"/>
  <xs:element name="container">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="container_no" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="container_no" type="xs:string"/>
  <xs:element name="procedure_code" type="xs:string"/>
  <xs:element name="goods_location">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:string" minOccurs="0"/>
        <xs:element name="name" type="xs:string" minOccurs="0"/>
        <xs:element name="type_code" type="xs:string" minOccurs="0"/>
        <xs:element name="address_type_code" type="xs:string" minOccurs="0"/>
        <xs:element ref="address" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="address_line" type="xs:string" minOccurs="0"/>
        <xs:element name="postal_code" type="xs:string" minOccurs="0"/>
        <xs:element name="type_code" type="xs:string" minOccurs="0"/>
        <xs:element name="city" type="xs:string" minOccurs="0"/>
        <xs:element name="country_code" type="xs:string" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="previousdocuments">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="previous_document" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="previous_document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="categorycode" type="xs:string" minOccurs="0"/>
        <xs:element name="id" type="xs:string" minOccurs="0"/>
        <xs:element name="typecode" type="xs:string" minOccurs="0"/>
        <xs:element name="linenumeric" type="xs:string" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="additionaldocuments">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="additional_document" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="additional_document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:string" minOccurs="0"/>
        <xs:element name="typecode" type="xs:string" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="authorisation_holder">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:string"/>
        <xs:element name="categorycode" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="test_flag" type="xs:boolean"/>
  <xs:element name="estimated_time_of_arrival" type="xs:dateTime"/>
  <xs:element name="packaging">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="packagemark" minOccurs="0" type="xs:string"/>
        <xs:element name="quantity" minOccurs="0" type="xs:string"/>
        <xs:element name="packages_type" minOccurs="0" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="integration_data">
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element ref="data"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="data" type="integration_data_type"/>
  <xs:complexType name="integration_data_type">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="id" type="xs:string"/>
        <xs:attribute name="type" type="xs:string"/>
        <xs:attribute name="description" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <!-- START Tariff unit -->
  <xs:element name="tariff_units">
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element ref="tariff_unit"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="tariff_unit" type="tariff_unit_type"/>
  
  <xs:complexType name="tariff_unit_type">
    <xs:simpleContent>
      <xs:extension base="positivedecimalnozero">
        <xs:attribute name="country" type="xs:string"/>
        <xs:attribute name="unit" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <!-- END Tariff unit -->


  <!-- HEAD TAG - PROCESS -->
  <xs:element name="process" type="xs:boolean"/>
  <!-- END HEAD TAG - PROCESS -->

  <!-- RULES -->
  <xs:element name="goodsstatuscode" type="xs:string"/>
  <xs:simpleType name="positivedecimalnozero">
    <xs:restriction base="xs:decimal">
      <xs:minExclusive value="0"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="positivedecimalzero">
    <xs:restriction base="xs:decimal">
      <xs:minInclusive value="0"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="validategoodsid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9]{10}|[0-9]{8}|()"/>
      <!-- Får vara tom, 8 eller 10 -->
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="validatecurrency">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{3}|()"/>
      <!-- A-Z 3tkn -->
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="validatecountrycode">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{2}|()"/>
      <!-- A-Z 2tkn -->
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
