0

This is a sample XML which I got from here: http://www.statistik.at/GBS-Schema/Beispiel/LHD-V55-Monat-Beispiel.xml

<?xml version="1.0" encoding="UTF-8"?>
<lhd:kennsatz   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:lhd="http://www.statistik.gv.at/LHD-V55/Schema"
                xsi:schemaLocation="http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd">
    <lhd:finanzjahr>2020</lhd:finanzjahr>
    <lhd:quartal>0</lhd:quartal>
    <lhd:monat>12</lhd:monat>
    <lhd:regkz>3</lhd:regkz>
    <lhd:periode>m</lhd:periode>
    <lhd:bundesland>Tirol</lhd:bundesland>
    <lhd:verantwortlich>max Hasenfuß</lhd:verantwortlich>
    <lhd:sachbearbeiter>Börti Fräli</lhd:sachbearbeiter>
    <lhd:telefon>02275/4525684</lhd:telefon>
    <lhd:email>boerti.fraenzli@steiermark.gv.at</lhd:email>
    <lhd:version>LHD-V5.5</lhd:version>
    <lhd:erstellt>2018-01-16</lhd:erstellt> 
    <lhd:finanzierungshaushalt>
        <lhd:ansatz_uab>770</lhd:ansatz_uab>
        <lhd:ansatz_ugl>000</lhd:ansatz_ugl>
        <lhd:konto>5880</lhd:konto>
        <lhd:konto_ugl>000</lhd:konto_ugl>
        <lhd:mvag_fhh>3126</lhd:mvag_fhh>
        <lhd:ord_aord>6</lhd:ord_aord>
        <lhd:sektor>0000</lhd:sektor>
        <lhd:ansatz_text>offizieller Ansatztext</lhd:ansatz_text>
        <lhd:konto_text>offizieller Kontotext</lhd:konto_text>
        <lhd:wert>9344974.61</lhd:wert>
    </lhd:finanzierungshaushalt> 
</lhd:kennsatz>  

This is the VBA-code I use:

Public Sub CheckLHDModule()

  Dim docum
  Set docum = CreateObject("MSXML2.DOMDocument.6.0")
  Call docum.Load("C:\Temp\LHD-V55-Monat-Beispiel.xml")
  Dim dParseError
  Set dParseError = docum.validate()
  If dParseError.ErrorCode <> 0 Then
    Call MsgBox("Fehler " & dParseError.ErrorCode & ": '" & dParseError.Reason & "' bei XML-Validierung. ")
  End If

  Set dParseError = docum.validateNode(docum.ChildNodes.Item(1))
  If dParseError.ErrorCode <> 0 Then
    Call MsgBox("Fehler " & dParseError.ErrorCode & ": '" & dParseError.Reason & "' bei XML-Validierung. ")
  End If

End Sub

Both validations return an error:

  • Validate returns:

    The node is neither valid nor invalid because no DTD/Schema declaration was found.
    (Original: "Der Knoten ist weder gültig noch ungültig, weil keine DTD/Schema-Deklaration gefunden wurde.")

  • validatenode returns:

    The element '{http://www.statistik.gv.at/LHD-V55/Schema}kennsatz' is used but not declared in the DTD/Schema."
    (Original: "Das Element '{http://www.statistik.gv.at/LHD-V55/Schema}kennsatz' wird verwendet, aber es ist im DTD/Schema nicht deklariert .")

The error code is -1072898035 in both cases.

But in http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd there is the element "kennsatz" defined. This is the current content of the file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:lhd="http://www.statistik.gv.at/LHD-V55/Schema"
            xmlns:t="http://www.statistik.gv.at/LHD-V55/Typen"
            targetNamespace="http://www.statistik.gv.at/LHD-V55/Schema"
            elementFormDefault="qualified">

    <!-- Typdefinitionen importieren -->
    <xs:import  schemaLocation="LHD-V55_Typdefinitionen.xsd"
                namespace="http://www.statistik.gv.at/LHD-V55/Typen"/> 
    <!-- Elementdefinitionen inkludieren -->
    <xs:include     schemaLocation="LHD-V55_Elementdefinitionen.xsd"/>

    <xs:include schemaLocation="LHD-V55_finanzierungshaushalt.xsd"/>

    <!-- Kennsatz -->

    <xs:element name="kennsatz"> 
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lhd:finanzjahr"/>

                <xs:element name="quartal">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="0"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element> 

                <xs:element name="monat">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value="[0][1-9]|[1][0-2]"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element> 
                <xs:element name="regkz" type="t:REGKZ"/>

                <xs:element name="periode">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value="m|M"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element> 

                <xs:element name="bundesland" type="t:BUNDESLAND"/>
                <xs:element name="verantwortlich" type="t:NAME"/> 
                <xs:element name="sachbearbeiter" type="t:NAME"/> 
                <xs:element ref="lhd:telefon"/>
                <xs:element ref="lhd:email"/> 
                <xs:element ref="lhd:version"/>
                <xs:element name="erstellt" type="xs:date"/>

                <!-- Kindelemente -->
                <xs:element ref="lhd:finanzierungshaushalt" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

When I validate "LHD-V55-Monat-Beispiel.xml" with notepad++ XML-Tools addon, the validation is successful. (MessageBox: "The xml is valid" ). So the file itself seems to be correct.

Do I miss a call? Why is the XML-Document not validated correctly?

Edits:

I tried to use:

xsi:schemaLocation="http://www.statistik.gv.at/LHD-V55/Schema http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd"

but it did not change anything.

Reduced example:

I changed the content of the xml to:

<?xml version="1.0" encoding="UTF-8"?>
<lhd:kennsatz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:lhd="http://www.statistik.gv.at/LHD-V55/Schema"
                xsi:schemaLocation="http://www.statistik.gv.at/LHD-V55/Schema file:///C:/RadLokal/trunk/Test/Access/MiniSchema.xsd">
                <lhd:finanzjahr>gogo</lhd:finanzjahr>
</lhd:kennsatz>  

And the content of the file: C:\RadLokal\trunk\Test\Access\MiniSchema.xsd is:

<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:lhd="http://www.statistik.gv.at/LHD-V55/Schema"
            targetNamespace="http://www.statistik.gv.at/LHD-V55/Schema"
            elementFormDefault="qualified">

    <xs:element name="kennsatz"> 
        <xs:complexType>
            <xs:sequence>
                <xs:element name="finanzjahr" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

The error is the same as before.

Gener4tor
  • 414
  • 3
  • 12
  • 40

2 Answers2

0

You're using xsi:schemaLocation as if it were xsi:noNamespaceSchemaLocation.

Instead of

xsi:schemaLocation="http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd"

use

xsi:schemaLocation="http://www.statistik.gv.at/LHD-V55/Schema
                    http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd"

That is, use a space-separated pair of

xsi:schemaLocation="NAMESPACE_VALUE SCHEMA_LOCATION"

See also How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?


In your reduced example, there are several problems, including at least:

  • The XSD lacks a targetNamespace.
  • The XSD references lhd:finanzjahr yet does not define it.
  • The XML does not use URL notation to reference the XSD.

My strong recommendation would be to break your problem down:

  1. Get a minimal XML file validating against a minimal XSD file with a known-working validator, not your own code.

  2. Get the above minimal pair working with your own code.

  3. Repeat 1-2 with your actual XML and XSD.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I'm sorry to say but using xsi:schemaLocation="xsi:schemaLocation="http://www.statistik.gv.at/LHD-V55/Schema http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd"" does not make any difference. Ill edit this into my question. – Gener4tor Jan 15 '20 at 13:29
  • The `xsi:schemaLocation` in your comment does not match that which I recommended in this answer. – kjhughes Jan 15 '20 at 13:55
  • @ kjhughes: This is because in the comments links are shown different. Look at the Question which I edited. This was the line I used. – Gener4tor Jan 15 '20 at 14:02
  • Ok, but note that you can (and should) use tick marks to format code in comments to avoid such trouble. – kjhughes Jan 15 '20 at 14:17
  • If this quick spot check fix isn't working for you, you'll need to create a [mcve] that exhibits the problem in a reproducible way. It's not reasonable to expect someone trying to help you to wade through or chase down numerous `import` and `include` dependencies (even if they were all accessible). – kjhughes Jan 15 '20 at 14:21
  • See "Reduced example:" at the end of my question. This should be a "minimal reproducible example". – Gener4tor Jan 15 '20 at 14:47
  • Your [mcve] is invalid. I cannot continue to guide you as this site is intended to be for Q/A, not extended consultation, but I've updated my answer to recommend a way forward. Good luck. – kjhughes Jan 15 '20 at 15:19
  • I edited my question. The xml in now valid - according to notepad++ - but the error is still the same. – Gener4tor Jan 15 '20 at 15:36
  • I solved my problem (See answer). You might want to adapt your answer. – Gener4tor Jan 16 '20 at 10:05
0

With the help of this Answer https://stackoverflow.com/a/11709428/9930052 I solved my problem. This is the code I use:

Dim objSchemaCache
Set objSchemaCache = CreateObject("MSXML2.XMLSchemaCache.6.0")

objSchemaCache.Add "http://www.statistik.gv.at/LHD-V55/Schema", "http://www.statistik.at/GBS-Schema/LHD/LHD-V55_kennsatz-monat.xsd"

Dim docum
Set docum = CreateObject("MSXML2.DOMDocument.6.0")
Call docum.Load("C:\Temp\LHD-V55-Monat-Beispiel.xml")
Set docum.Schemas = objSchemaCache
Dim dParseError

Set dParseError = docum.validate()
Stop
If dParseError.ErrorCode <> 0 Then
Call MsgBox("Fehler " & dParseError.ErrorCode & ": '" & dParseError.Reason & "' bei XML-Validierung. ")
End If

Set dParseError = docum.validateNode(docum.ChildNodes.Item(1))
Stop
If dParseError.ErrorCode <> 0 Then
Call MsgBox("Fehler " & dParseError.ErrorCode & ": '" & dParseError.Reason & "' bei XML-Validierung. ")
End If

You have to load the Schema seperatly into a XMLSchemaCache and set this in the DOMDocument.

Gener4tor
  • 414
  • 3
  • 12
  • 40