﻿<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns="http://schema.webpdf.de/1.0/report/invoice" elementFormDefault="qualified"
           targetNamespace="http://schema.webpdf.de/1.0/report/invoice" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:annotation>
        <xs:documentation>
            <p>The invoice rule validation report types. This schema is self-contained: it has its own
                namespace and its own root element, so an invoice rule validation result can be read and
                validated on its own. "report/pdfa.xsd" imports this schema and appends an additional,
                optional "invoice" element - of the "InvoiceValidationResultType" defined here - at the
                end of its own report sequence.
            </p>
        </xs:documentation>
    </xs:annotation>
    <xs:element name="report" type="InvoiceValidationResultType">
        <xs:annotation>
            <xs:documentation>
                <p>The standalone root element for an invoice rule validation result, of type
                    "InvoiceValidationResultType". It is the response document of the "pdfa" web
                    service's "invoice" operation, which validates an invoice XML without a PDF carrier.
                    The "convert" and "analyze" operations instead reference "invoice" (below) - of the
                    same type - as a child of their own report (see "report/pdfa.xsd").
                </p>
            </xs:documentation>
        </xs:annotation>
    </xs:element>
    <xs:element name="invoice" type="InvoiceValidationResultType">
        <xs:annotation>
            <xs:documentation>
                <p>The element "report/pdfa.xsd" references (via "xs:element ref", not a local
                    declaration of its own) to append the invoice rule validation result as a child of
                    its own report. The embedded instance is therefore qualified with THIS schema's own
                    "invoice" namespace, not with the "pdfa" report's namespace - a reader of that
                    report addresses it accordingly.
                </p>
            </xs:documentation>
        </xs:annotation>
    </xs:element>
    <xs:complexType name="InvoiceValidationResultType">
        <xs:annotation>
            <xs:documentation>
                <p>The outcome of an invoice rule validation, modelled after the existing finding-counter
                    pattern of "PdfaAnalysisType" (report/pdfa.xsd).
                </p>
                <p>"state" is webPDF's own verdict, derived from whether a rule package could be
                    evaluated at all ("notEvaluable") and, if so, from that package's own "assessment"
                    ("compliant" for "accept", "nonCompliant" for "reject"). "assessment" and "reason"
                    are mutually exclusive in practice: "assessment" is present only when a rule package
                    was actually evaluated (state "compliant" or "nonCompliant"), "reason" only when it
                    was not (state "notEvaluable").
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="ruleSet" type="InvoiceRuleSetInfoType" minOccurs="0" maxOccurs="1">
                <xs:annotation>
                    <xs:documentation>
                        <p>The rule package the verdict is based on. Present whenever a package could be
                            loaded, even when no scenario of it matched the document.
                        </p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="finding" type="InvoiceFindingType" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>
                        <p>An individual finding of the rule package, in document order.</p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="validationStep" type="InvoiceValidationStepType" minOccurs="0"
                        maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>
                        <p>One entry per validation step of the rule package that was actually
                            EXECUTED, in the order the package reports them - together they say which
                            checks the verdict rests on.
                        </p>
                        <p>A step that did not run is ABSENT rather than reported as failed: when the
                            rule package's schema step rejects a document, its business-rule steps are
                            never performed at all, and reporting them - with any outcome - would claim
                            a check that never happened. A step whose outcome the rule package's report
                            does not state readably is absent for the same reason - "not determinable"
                            is not "not passed", and "valid" below has no third value to say so. Absent
                            as a whole whenever no rule package evaluation produced a report at all
                            (every "state=notEvaluable" outcome, whatever its reason); its absence
                            therefore carries no verdict information of its own.
                        </p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="document" type="InvoiceDocumentInfoType" minOccurs="0" maxOccurs="1">
                <xs:annotation>
                    <xs:documentation>
                        <p>Identifies the invoice document the verdict is about - its human-readable
                            reference and its cryptographic digest - so a stored verdict stays bound to
                            the document it was reached for.
                        </p>
                        <p>Present only when a rule package evaluation produced a report; a reference
                            or a digest reported for a document that was never evaluated would look
                            exactly like evidence and be none. Its ABSENCE therefore says nothing about
                            the verdict itself - "state", "assessment", "reason" and the finding
                            counters are unaffected by it.
                        </p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="originalReport" type="InvoiceOriginalReportDataType" minOccurs="0" maxOccurs="1">
                <xs:annotation>
                    <xs:documentation>
                        <p>The rule package's OWN report (VARL - the KoSIT/XRechnung "Validation Artefact
                            Result List"), delivered base64-encoded exactly as the rule package produced
                            it, including its own XML declaration.
                        </p>
                        <p>Present only when it was both explicitly requested - through
                            "invoiceRuleValidation/@originalReport" of the "pdfa" web service, which
                            carries the same name as this element - AND a rule package evaluation
                            actually produced a report. Its ABSENCE by itself carries no
                            verdict information: it is absent whenever the payload was not requested,
                            whenever no rule package evaluation ran at all (every "state=notEvaluable"
                            outcome, whatever its reason), whenever the raw, not-yet-encoded payload
                            exceeded the operator's configured size ceiling, and whenever the payload
                            could not be produced for any other reason - producing it is never allowed
                            to fail the request, so a failure removes the payload and nothing else. In
                            every one of these cases "state", "assessment", "reason" and the finding
                            counters are unaffected.
                        </p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="originalReportHtml" type="InvoiceOriginalReportDataType" minOccurs="0"
                       maxOccurs="1">
                <xs:annotation>
                    <xs:documentation>
                        <p>The human-readable HTML view the rule package embeds in its OWN report,
                            delivered base64-encoded as a payload of its own, EXTRACTED from that report
                            rather than re-rendered - the same document, not a webPDF reconstruction of
                            it.
                        </p>
                        <p>Present only when it was both explicitly requested - through
                            "invoiceRuleValidation/@originalReportHtml" of the "pdfa" web service, which
                            carries the same name as this element - AND "originalReport" (above) asked
                            for a delivery, AND a rule package evaluation produced a report that embeds
                            such a view. That the view is taken out of that report does NOT make its
                            delivery depend on the report being delivered too: the operator's size
                            ceiling is measured against each raw payload SEPARATELY, so a ceiling that
                            suppresses the (larger) report can still let the (smaller) view through, and
                            this element can therefore be present while "originalReport" is absent.
                        </p>
                        <p>Its ABSENCE by itself carries no verdict information: it is absent whenever
                            the view was not requested, whenever "originalReport" asked for no delivery
                            at all, whenever no rule package evaluation ran (every "state=notEvaluable"
                            outcome, whatever its reason), whenever the produced report embeds no view
                            carrying the marker this extraction selects by, whenever the view's OWN raw,
                            not-yet-encoded size exceeded the operator's configured size ceiling, and
                            whenever it could not be produced for any other reason. In every one of
                            these cases "state", "assessment", "reason" and the finding counters are
                            unaffected.
                        </p>
                        <p>The view is delivered UTF-8 encoded, exactly as the report embeds it, and
                            therefore carries no character-set declaration of its own: it is a fragment
                            taken out of an XML document, not a standalone HTML file. A recipient that
                            stores or serves it as HTML has to state UTF-8 itself - through the
                            "Content-Type" of the response, or by adding a "meta" element - because an
                            HTML parser ignores an XML declaration and would otherwise fall back to its
                            own default encoding and render non-ASCII invoice content incorrectly.
                        </p>
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="state" type="InvoiceStateType" use="required">
            <xs:annotation>
                <xs:documentation>
                    <p>webPDF's own verdict: "compliant", "nonCompliant" or "notEvaluable".</p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="assessment" type="InvoiceAssessmentType" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>The verdict of the rule package itself ("accept" / "reject"). Present only when a
                        scenario of a rule package actually matched the document.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="reason" type="InvoiceNotEvaluableReasonType" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>Present only for "state=notEvaluable": why the document could not be assessed.</p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="error" type="xs:int" use="required">
            <xs:annotation>
                <xs:documentation><p>Number of findings whose applied level is "error".</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="warning" type="xs:int" use="required">
            <xs:annotation>
                <xs:documentation><p>Number of findings whose applied level is "warning".</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="information" type="xs:int" use="required">
            <xs:annotation>
                <xs:documentation><p>Number of findings whose applied level is "information".</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="timestamp" type="xs:dateTime" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>When the rule package evaluation was performed, with a UTC offset - the question
                        a stored report cannot answer otherwise, because a rule package update can
                        change the outcome for the very same document.
                    </p>
                    <p>Present only when a rule package evaluation produced a report, and taken from
                        that report rather than read from a second clock. Absent for every
                        "state=notEvaluable" outcome, whatever its reason; do not read its absence as a
                        statement about the verdict.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="enforcement" type="InvoiceEnforcementType" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>The enforcement decision of a "mode=enforce" request, using the SAME predicate
                        the "convert" branch applies to reject an invoice ("passed" when the document
                        would NOT be rejected, "rejected" when it would). Present only when
                        "mode=enforce" was actually requested - its ABSENCE means enforcement was not
                        requested; do not infer "passed" from its absence.
                    </p>
                    <p>Set by the "analyze" and "invoice" operations only. The "convert" branch does not
                        set this attribute, because there a rejection under "mode=enforce" is already
                        observable as the operation's own error code (PDFA_ZUGFERD_RULE_VALIDATION_ERROR)
                        instead of an embedded invoice; "analyze" and "invoice" have no equivalent error
                        code (the operation always returns its report), so this attribute is their only
                        carrier of that decision.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
    <xs:complexType name="InvoiceRuleSetInfoType">
        <xs:annotation>
            <xs:documentation>
                <p>Identifies the rule package release a verdict is based on - required so a verdict
                    stays reproducible (a rule package update can change the outcome for the very same
                    document).
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="id" type="xs:string" use="required">
            <xs:annotation>
                <xs:documentation><p>The rule package identifier ("xrechnung" / "zugferd", or an
                    operator-configured identifier).</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="name" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation><p>The display name the rule package declares.</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="release" type="xs:string" use="required">
            <xs:annotation>
                <xs:documentation><p>The release date the rule package declares (as found in the package
                    itself, not the distribution tag).</p></xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
    <xs:complexType name="InvoiceFindingType">
        <xs:annotation>
            <xs:documentation>
                <p>A single finding of the rule package. The element content is the finding's message
                    text.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="code" type="xs:string" use="required">
                    <xs:annotation>
                        <xs:documentation><p>The rule identifier, or the Xerces error code for a schema
                            finding.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="level" type="InvoiceLevelType" use="required">
                    <xs:annotation>
                        <xs:documentation><p>The APPLIED level, after the rule package's own optional
                            "customLevel" remapping. This is what a "state"/counter decision is based
                            on.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="reportedLevel" type="InvoiceLevelType" use="required">
                    <xs:annotation>
                        <xs:documentation><p>The RAW level the rule package reported, before any
                            "customLevel" remapping.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="sourceSeverity" type="xs:string" use="optional">
                    <xs:annotation>
                        <xs:documentation><p>Optional: the source severity the rule artefact itself
                            assigned (Schematron "@flag" / "@role"), when the artefact assigns one. Its
                            ABSENCE is itself information - it means the underlying rule artefact assigns
                            no source severity for this finding, which is why this attribute is optional
                            rather than defaulted.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="step" type="xs:string" use="optional">
                    <xs:annotation>
                        <xs:documentation><p>The validation step the finding belongs to
                            ("val-xml" wellformedness, "val-xsd" the rule package's own schema step,
                            "val-sch.&lt;n&gt;" a Schematron step) - what distinguishes a schema finding
                            from a business-rule finding.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="xpathLocation" type="xs:string" use="optional">
                    <xs:annotation>
                        <xs:documentation><p>Optional: the XPath location of the finding within the
                            invoice document.</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="lineNumber" type="xs:int" use="optional">
                    <xs:annotation>
                        <xs:documentation><p>Optional: the line number of the finding, when known (schema
                            findings only).</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="columnNumber" type="xs:int" use="optional">
                    <xs:annotation>
                        <xs:documentation><p>Optional: the column number of the finding, when known
                            (schema findings only).</p></xs:documentation>
                    </xs:annotation>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="InvoiceValidationStepType">
        <xs:annotation>
            <xs:documentation>
                <p>One executed validation step of the rule package: which step it was, and whether the
                    document passed it.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="id" type="xs:string" use="required">
            <xs:annotation>
                <xs:documentation>
                    <p>The step identifier, from the SAME vocabulary "InvoiceFindingType/@step" uses
                        ("val-xml" wellformedness, "val-xsd" the rule package's own schema step,
                        "val-sch.&lt;n&gt;" a Schematron step) - so a finding can be attributed to the
                        step that produced it without a translation table.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="valid" type="xs:boolean" use="required">
            <xs:annotation>
                <xs:documentation>
                    <p>Whether the document passed THIS step. It is not the overall verdict: a document
                        can fail an individual step and still be officially accepted, and the verdict
                        remains "state"/"assessment" on "InvoiceValidationResultType".
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
    <xs:complexType name="InvoiceDocumentInfoType">
        <xs:annotation>
            <xs:documentation>
                <p>Identifies the checked invoice document, bundling the identity attributes in one
                    element the same way the "ruleSet" element bundles the rule package's identity.
                </p>
                <p>Every attribute below is taken from the rule package's own report rather than
                    composed a second time, so this element and that report can never state different
                    facts about the same document. Each is optional because the report states it: an
                    attribute is ABSENT exactly when that report carries no such value, and its
                    absence is never a statement about the verdict.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="reference" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>A human-readable reference to what was checked: the name of the embedded
                        attachment the invoice was taken from, when one is known, followed by the
                        processing job reference. Where no attachment name exists - a document handed
                        in directly rather than extracted from a PDF - only the job reference is
                        given. Never derived from a request URI, which can carry credentials or
                        internal host names.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="hash" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>The base64-encoded digest of the exact invoice bytes that were checked - what
                        binds this verdict to the document the sender actually sent. Read together with
                        "hashAlgorithm", never on its own.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="hashAlgorithm" type="xs:string" use="optional">
            <xs:annotation>
                <xs:documentation>
                    <p>The algorithm that produced "hash", named explicitly rather than assumed, so a
                        later algorithm can never silently reinterpret a digest that was already
                        delivered.
                    </p>
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
    <xs:complexType name="InvoiceOriginalReportDataType">
        <xs:annotation>
            <xs:documentation>
                <p>Carries a base64-encoded payload and nothing else - the same shape as
                    "extraction/barcode.xsd"'s "DataType". Used for a report payload, never for a
                    request: unlike the file-carrying request types of the operation schemas, an instance
                    of this type never references anything other than its own inline content.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:simpleContent>
            <xs:extension base="xs:base64Binary"/>
        </xs:simpleContent>
    </xs:complexType>
    <xs:simpleType name="InvoiceStateType">
        <xs:annotation>
            <xs:documentation>
                <p>webPDF's own verdict, exhaustive by construction (evaluable × the rule package's own
                    verdict): "compliant" for an evaluated, accepted document, "nonCompliant" for an
                    evaluated, rejected document, "notEvaluable" for every case in which no rule package
                    verdict could be produced at all (see "InvoiceNotEvaluableReasonType" for why).
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="compliant"/>
            <xs:enumeration value="nonCompliant"/>
            <xs:enumeration value="notEvaluable"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="InvoiceEnforcementType">
        <xs:annotation>
            <xs:documentation>
                <p>The result of applying a "mode=enforce" request's rejection predicate to an invoice
                    validation outcome: "passed" when the document would not be rejected, "rejected"
                    when it would (see "InvoiceValidationResultType/@enforcement").
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="passed"/>
            <xs:enumeration value="rejected"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="InvoiceAssessmentType">
        <xs:annotation>
            <xs:documentation>
                <p>The rule package's own verdict - "accept" or "reject" - taken verbatim from its
                    official report, never re-derived from the individual findings. There is no third
                    value such as "accepted with warnings": a document whose findings are exclusively
                    "warning"/"information" can be officially "accept", and its "invoice" element is then
                    embedded ("mode=enforce" does not reject it) while still carrying its "finding"
                    entries and a non-zero "warning"/"information" counter on
                    "InvoiceValidationResultType". Whether an accepted document actually had findings is
                    visible only through those counters, never through "assessment" itself.
                </p>
                <p>The two vendored rule packages differ here: with "xrechnung" a finding can be reported
                    as "warning" or "information" and still leave the document accepted. With "zugferd"
                    every finding is reported as "error", so a "zugferd" verdict is "reject" as soon as
                    the invoice has any finding at all - there is no "accepted with warnings" under this
                    package.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="accept"/>
            <xs:enumeration value="reject"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="InvoiceLevelType">
        <xs:annotation>
            <xs:documentation>
                <p>Every loaded rule package must declare "scenarios/@frameworkVersion" as "1.x"; a
                    package declaring any other value is not loaded at all. This closed value range is
                    tied to that check.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="error"/>
            <xs:enumeration value="warning"/>
            <xs:enumeration value="information"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="InvoiceNotEvaluableReasonType">
        <xs:annotation>
            <xs:documentation>
                <p>Why "state=notEvaluable": no rule package was available at all ("noRuleSet"), the
                    selected rule package(s) had no scenario matching the document ("noScenario"), a
                    configured size or nesting-depth limit was exceeded ("documentTooLarge" /
                    "documentTooDeep"), the soft time budget elapsed before a verdict could be produced
                    ("timeBudgetExceeded"), the configured rule package could not be loaded/compiled at
                    all - a broken or missing "scenarios.xml", an unsupported "frameworkVersion", or a
                    resource reference refused by the fail-closed resolver ("ruleSetLoadError"), or an
                    unexpected engine failure ("engineError"). No embedded attachment qualified as the
                    invoice ("noInvoiceAttachment"), more than one attachment qualified equally
                    ("ambiguousInvoiceAttachment"), more attachment candidates were present than the
                    configured cap allows ("tooManyAttachments"), the chosen attachment's raw, still
                    compressed size exceeded the configured cap ("invoiceAttachmentTooLarge"), the
                    chosen attachment could not be decoded safely or is not well-formed XML
                    ("invoiceAttachmentUnreadable"), or the carrying PDF document itself could not be
                    opened or decrypted - for the "invoice" operation: the source document, which is
                    the invoice itself, is not well-formed XML ("sourceDocumentUnreadable"). "other" is
                    the open fallback for a cap or engine state none of the above names.
                </p>
                <p>Naming convention: "document" refers to the invoice document (the XML) being
                    validated, "sourceDocument" to the PDF that carries it (for the "invoice" operation:
                    the source document of the request, which is the invoice XML itself), and
                    "invoiceAttachment" to the embedded file stream before it is read as that document.
                </p>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="noRuleSet"/>
            <xs:enumeration value="noScenario"/>
            <xs:enumeration value="documentTooLarge"/>
            <xs:enumeration value="documentTooDeep"/>
            <xs:enumeration value="timeBudgetExceeded"/>
            <xs:enumeration value="ruleSetLoadError"/>
            <xs:enumeration value="engineError"/>
            <xs:enumeration value="noInvoiceAttachment"/>
            <xs:enumeration value="ambiguousInvoiceAttachment"/>
            <xs:enumeration value="tooManyAttachments"/>
            <xs:enumeration value="invoiceAttachmentTooLarge"/>
            <xs:enumeration value="invoiceAttachmentUnreadable"/>
            <xs:enumeration value="sourceDocumentUnreadable"/>
            <xs:enumeration value="other"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
