<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns:tns="http://minameddelanden.gov.se/schema/Common/v4"
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://minameddelanden.gov.se/schema/Common/v4"
    elementFormDefault="qualified"
    version="4.0">

    <annotation>
        <documentation xml:lang="en">Element definitions and schema data types that are common and re-used
            in other schemas.
        </documentation>
    </annotation>

    <!-- Identification base elements -->

    <simpleType name="swedishPersonalNumberType">
        <annotation>
            <documentation xml:lang="en">Swedish Personal Identity Number</documentation>
            <documentation xml:lang="sv">Svenskt personnummer, fysisk person.</documentation>
        </annotation>
        <restriction base="string">
            <length value="12"/>
            <pattern value="((18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4})"/>
        </restriction>
    </simpleType>

    <simpleType name="swedishCoordinationNumberType">
        <annotation>
            <documentation xml:lang="en">Swedish Co-ordination Number.</documentation>
            <documentation xml:lang="sv">Svenskt samordningsnummer.</documentation>
        </annotation>
        <restriction base="string">
            <length value="12"/>
            <pattern value="((18|19|20)\d{2}(0[1-9]|1[0-2])(\d{2})\d{4})"/>
        </restriction>
    </simpleType>

    <simpleType name="swedishOrganizationalNumberType">
        <annotation>
            <documentation xml:lang="en">Swedish Corporate Identity Number. Legal entity.</documentation>
            <documentation xml:lang="sv">Svenskt organisationsnummer, juridisk person.</documentation>
        </annotation>
        <restriction base="string">
            <length value="12"/>
            <pattern value="(16\d{10})"/>
        </restriction>
    </simpleType>

    <element name="swedishUserIdentity">
        <!-- Was http://minameddelanden.gov.se/schema/Authority schema/Auhtority.xsd UserIdentity earlier -->
        <annotation>
            <documentation xml:lang="en">This identification is a looser definition of a logged-in user,
                it can either be a personal number (a less restricted version) or a fairly arbitrary
                string as long as it begins with 2 letters and is at least 12 characters long.
            </documentation>
            <documentation xml:lang="sv">Detta är en definition av en inloggad användare,
                det kan antingen vara ett personnummer eller en arbiträr sträng 2-12 tecken. Användningen
                är de tillfällen då en strikt personnummerkontroll inte är nödvändig.
            </documentation>
        </annotation>
        <simpleType>
            <restriction base="string">
                <minLength value="12"/>
                <maxLength value="255"/>
                <pattern
                    value="((18|19|20)\d{10})|([a-z,A-Z]{2}.{10,253})">
                </pattern>
            </restriction>
        </simpleType>
    </element>

    <!-- Identification elements -->

    <element name="swedishId">
        <!-- Was http://minameddelanden.gov.se/schema/Recipient schema/Recipient.xsd RecipientId earlier -->
        <annotation>
            <documentation xml:lang="en">General Swedish identification, can be
                a person with personal number or coordination number or an organization
                with an organizational number.
            </documentation>
            <documentation xml:lang="sv">En generisk identitet för svenska personnummer, samordningsnummer
                och organisationsnummer.
            </documentation>
        </annotation>
        <simpleType>
            <union
                memberTypes="tns:swedishPersonalNumberType tns:swedishCoordinationNumberType tns:swedishOrganizationalNumberType"/>
        </simpleType>
    </element>

    <element name="swedishOrganizationId" type="tns:swedishOrganizationalNumberType">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Ett svenskt organisationsnummer.</documentation>
        </annotation>
    </element>

    <element name="foreignId">
        <annotation>
            <documentation xml:lang="en">This identification is made to allow for foreign residents
                and organizations to participate. The Single Digital Gateway initiative is the base
                for this definition, and the definition is subject to changes.

                See section 2.5 in
                https://docs.swedenconnect.se/technical-framework/mirror/eidas/eIDAS_SAML_Attribute_Profile_v1.2-FINAL.pdf
            </documentation>
            <documentation xml:lang="sv">Identifikation för gränsöverskridande mottagare och organisationer.
                EU-förordningen Single Digital Gateway är grunden för denna definition, och den kan komma att
                förändras.

                Se även sektion 2.5 i:
                https://docs.swedenconnect.se/technical-framework/mirror/eidas/eIDAS_SAML_Attribute_Profile_v1.2-FINAL.pdf
            </documentation>
        </annotation>
        <simpleType>
            <restriction base="string">
                <maxLength value="256"/>
            </restriction>
        </simpleType>
    </element>

    <!-- Compound identification elements -->

    <element name="recipientId">
        <annotation>
            <documentation xml:lang="en">Either a recipient ID for Swedish recipients
                (Swedish personal identity number or Swedish organization number),
                or a foreign ID for persons and organizations that lack Swedish identification.
            </documentation>
            <documentation xml:lang="sv">Antingen ett mottagar-ID för svenska mottagare
                (svensk personnummer, samordningsnummer eller organisationsnummer), eller ett
                gränsöverskridande ID för personer och organisationer som saknar svensk identitet.
            </documentation>
        </annotation>
        <complexType>
            <choice>
                <element ref="tns:swedishId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Mottagare med svensk identitet.</documentation>
                    </annotation>
                </element>
                <element ref="tns:foreignId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Mottagare med gränsöverskridande identitet.</documentation>
                    </annotation>
                </element>
            </choice>
        </complexType>
    </element>

    <element name="userId">
        <annotation>
            <documentation xml:lang="en">The identification for a logged-in user,
                either a Swedish identity or a foreign identity.
            </documentation>
            <documentation xml:lang="sv">Identifikation av en inloggad användare, kan vara en svensk
                eller gränsöverskridande identitet.
            </documentation>
        </annotation>
        <complexType>
            <choice>
                <element ref="tns:swedishUserIdentity">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Användare med svensk identitet.</documentation>
                    </annotation>
                </element>
                <element ref="tns:foreignId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Användare med gränsöverskridande identitet.</documentation>
                    </annotation>
                </element>
            </choice>
        </complexType>
    </element>

    <complexType name="signerType">
        <annotation>
            <documentation xml:lang="en">A legal entity with the capability to sign documents.</documentation>
            <documentation xml:lang="sv">En juridisk person, med möjlighet att signera (firmatecknare).
                Kan antingen vara en svensk eller gränsöverskridande entitet.
            </documentation>
        </annotation>
        <choice>
            <sequence id="swedishSigner">
                <element ref="tns:swedishId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Användare med svensk identitet.</documentation>
                    </annotation>
                </element>
                <element name="name" type="string">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Användarens namn.</documentation>
                    </annotation>
                </element>
            </sequence>
            <element ref="tns:foreignId" id="foreignSigner">
                <annotation>
                    <documentation xml:lang="en"></documentation>
                    <documentation xml:lang="sv">Användare med gränsöverskridande identitet.</documentation>
                </annotation>
            </element>
        </choice>
    </complexType>

    <element name="organizationId">
        <annotation>
            <documentation xml:lang="en">Identity of a legal entity (organization).</documentation>
            <documentation xml:lang="sv">Identitet för en juridisk person (organisation).</documentation>
        </annotation>
        <complexType>
            <choice>
                <element ref="tns:swedishOrganizationId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Svensk organisationsidentitet.</documentation>
                    </annotation>
                </element>
                <element ref="tns:foreignId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Gränsöverskridande organisationsidentitet.</documentation>
                    </annotation>
                </element>
            </choice>
        </complexType>
    </element>

    <element name="senderId">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Identitet för och information om en avsändare.</documentation>
        </annotation>
        <complexType>
            <sequence>
                <element ref="tns:swedishOrganizationId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Avsändarens organisationsnummer.</documentation>
                    </annotation>
                </element>
                <element name="name" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Avsändarens organisationsnamn.</documentation>
                    </annotation>
                    <simpleType>
                        <restriction base="string">
                            <minLength value="2"/>
                            <maxLength value="256"/>
                        </restriction>
                    </simpleType>
                </element>
                <element name="attention" type="string" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Underadress, kan tex vara en avdelning dit avsändaren
                            vill att svar skall hamna.
                        </documentation>
                    </annotation>
                </element>
            </sequence>
        </complexType>
    </element>

    <element name="dispatcherId">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Identitet för och information om en förmedlare.</documentation>
        </annotation>
        <complexType>
            <sequence>
                <element ref="tns:swedishOrganizationId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Förmedlarens organisationsnummer.</documentation>
                    </annotation>
                </element>
                <element name="name" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Förmedlarens namn.</documentation>
                    </annotation>
                    <simpleType>
                        <restriction base="string">
                            <minLength value="2"/>
                            <maxLength value="256"/>
                        </restriction>
                    </simpleType>
                </element>
                <!-- This is changed from just an organization id for sender to be the whole sender object.
                     If this proves to be a problem, the type swedishOrganizationalNumberType could be used
                     instead. In theory this could reduce the number of calls to getSenders. -->
                <element ref="tns:senderId" minOccurs="0" maxOccurs="unbounded">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Lista på organisationsnummer för de
                            avsändare som denne förmedlare är godkänd att signera för.
                        </documentation>
                    </annotation>
                </element>
            </sequence>
        </complexType>
    </element>

    <element name="serviceSupplierId">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Information för en brevlådeoperatör.</documentation>
        </annotation>
        <complexType>
            <sequence>
                <element ref="tns:swedishOrganizationId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Brevlådeoperatörens organisationsnummer.</documentation>
                    </annotation>
                </element>
                <element name="name" type="string">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Brevlådeoperatörens namn.</documentation>
                    </annotation>
                </element>
                <element name="logo" type="anyURI" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">URL som pekar på en bild av brevlådeoperatörens logo.
                        </documentation>
                    </annotation>
                </element>
                <element name="serviceAddress" type="anyURI">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Adressen är en WS-endpoint till tjänsten hos den operatör
                            som mottagaren valt för sin brevlåda. Se även dokumentet: Klientkonfiguration.
                        </documentation>
                    </annotation>
                </element>
                <element name="uiAddress" type="anyURI" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Basadress för direktnavigering i e-tjänsten hos den operatör
                            som mottagaren valt för sin brevlåda och profil. Värdet är ’null’ om
                            mottagarens e-tjänst-operatör inte stödjer direktnavigering.
                        </documentation>
                    </annotation>
                </element>
            </sequence>
        </complexType>
    </element>

    <element name="signatureData">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">En signatur som skapats med ett certifikat som bifogas till det data som
                signerats.
            </documentation>
        </annotation>
        <complexType>
            <sequence>
                <element name="signature" type="base64Binary">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">En Base64 kodad signatur enligt formaten PKCS#7 eller
                            XML-signature.
                        </documentation>
                    </annotation>
                </element>
                <element name="certificate" type="string">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Namn på organisationen som givit ut certifikatet som
                            användes vid skapandet av signaturen.
                        </documentation>
                    </annotation>
                </element>
                <element name="nonce" type="string" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Slumpat ord som användes vid skapandet av en
                            XML-signatur. Endast BankID:s PKI-klient.
                        </documentation>
                    </annotation>
                </element>
                <element name="encodingOfSignedText" type="string" default="UTF-8" minOccurs="0">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Encoding av texten/datat som signerats.
                            Default: UTF-8.
                        </documentation>
                    </annotation>
                </element>
            </sequence>
        </complexType>
    </element>

    <element name="applicationFaultDetail">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Information kring ett fel som uppstod vid ett anrop av en tjänsteoperation.
            </documentation>
        </annotation>
        <complexType>
            <sequence>
                <element name="errorCode">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Felkod.</documentation>
                    </annotation>
                    <simpleType>
                        <restriction base="int">
                            <minExclusive value="0"/>
                            <maxExclusive value="9999"/>
                        </restriction>
                    </simpleType>
                </element>
                <element name="description" type="string">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Text som ev. kan presenteras för en slutanvändare.</documentation>
                    </annotation>
                </element>
                <element name="callId">
                    <annotation>
                        <documentation xml:lang="en"></documentation>
                        <documentation xml:lang="sv">Kod som kan användas för att spåra ett fel.</documentation>
                    </annotation>
                    <simpleType>
                        <restriction base="string">
                            <minLength value="20"/>
                            <maxLength value="50"/>
                        </restriction>
                    </simpleType>
                </element>
            </sequence>
        </complexType>
    </element>

    <simpleType name="emailAddress">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">En giltig e-post-adress enligt RFC 5322 (ietf) avsnitt 3.4.1. Exempel:
                kalle@gmail.com
            </documentation>
        </annotation>
        <restriction base="string">
            <minLength value="5"/>
            <maxLength value="256"/>
            <pattern value=".+@.+\..+"/>
        </restriction>
    </simpleType>

    <element name="messageId" type="string">
        <annotation>
            <documentation xml:lang="en">A unique identification of a message. This is used to uniquely
                identify a message throughout the infrastructure, independent of the intermediary actors
                that handles a message. It originates from the sender.

                Typically, this should be a UUID,
                see: https://www.itu.int/ITU-T/recommendations/rec.aspx?rec=11746&amp;lang=en
            </documentation>
            <documentation xml:lang="sv">En unik identifierare av ett meddelande. Detta kan användas
                för att unikt identifiera ett meddelande genom hela infrastrukturen och på så sätt
                spåra ett meddelandes händelseförlopp, oberoende av de aktörer som agerat som
                mellanhänder. ID't sätt och har sitt upphof hos avsändaren.

                Typiskt skall detta vara ett UUID,
                Se även: https://www.itu.int/ITU-T/recommendations/rec.aspx?rec=11746&amp;lang=en
            </documentation>
        </annotation>
    </element>

    <element name="timestamp" type="date">
        <annotation>
            <documentation xml:lang="en"></documentation>
            <documentation xml:lang="sv">Tidsstämpel.</documentation>
        </annotation>
    </element>
</schema>
