'Validate XML against schema with delphi

Hell everyone,

I'm trying to validate XML against schema using the delphi procedure, but I'm getting sort of errors like: the node is not correct neither incorrect - cannot find DTD definition.

I'm not sure if I'm providing the procedure with proper order of schema files neither if the procedure is correct.

I hope anyone could help me with this.

Here is my delphi procedure

uses MSXML2_TLB;

procedure TForm1.btn_1Click(Sender: TObject);
Var Schemadoc, Xmldoc : ixmldomdocument2;
    Schemacache : ixmldomschemacollection;
    Error : ixmldomparseerror;
Begin
  XmlDoc := codomdocument60.create;
  Xmldoc.async := False;
  Xmldoc.load('file-to-test.xml');

  Schemadoc := codomdocument60.create;
  Schemadoc.async := False;
  Schemadoc.load('typy_danych.xsd');

  SchemaCache := coxmlschemacache60.create;
  Schemacache.add('smpwc.xsd', Schemadoc);

  Xmldoc.schemas := SchemaCache;

  Error := xmldoc.validate;
  If Error.errorcode <> S_OK
    Then mem_1.Lines.add (Error.reason)
    Else mem_1.Lines.add (' verification success ');
end;

MSXML2 bases on Microsoft XML v6.0.

Here is my XML which contains error that I want to detect with my procedure. The error is where I placed '-here-is-error-'.

The patient's / doctor's IDs are because of data protection reasons random, but from the schema point of view - correct.

<?xml version="1.0" encoding="windows-1250"?>
<komunikat xmlns="www.csioz.gov.pl/xml/swd-platnik/1" xmlns:nfz="www.nfz.gov.pl/xml/swd-platnik/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.csioz.gov.pl/xml/swd-platnik/1 smpwc.xsd" wersja="1" nfz:wersja="1.0" typ="SMPWC" id-odb="13" id-inst-odb="SIMP" id-nad="81465" id-inst-nad="Patomorfolog" nr-gen="377" czas-gen="2021-08-10T09:29:04" nfz:info-aplik-nad="Patomorfolog" nfz:info-kontakt-nad="NZOZ Patomorfolog">
  <swiadczeniodawca typ-id-swd="X" id-swd="81465" id-inst="MyId" nfz:info-kontakt="61 123456"/>
  <zest-wyn-cyt nfz:umowa="13-00-12345-21-01" rok="2021">
    <poz-umowy id-miej-wyk-swiad="8721" nr-pkt-umowy="1">
      <badanie id-bad="9536-here-is-error-768" pesel-pacj="82020477523">
        <wyniki-bad data-otrzym-prep="2021-07-23" data-wyniku="2021-07-27" nr-wyniku="00000/21/G" wyniki-rozmazu="A20B1B3C254"/>
        <personel-bad pesel-cytot="72081092874"/>
      </badanie>
    </poz-umowy>
  </zest-wyn-cyt>
</komunikat>

I'm given with three files that should let me validate the XML: smp_nfz.xsd, smpwc.xsd, typy_danych.xsd.

They are here

smp_nfz.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="www.csioz.gov.pl/xml/swd-platnik/1" xmlns:nfz="www.nfz.gov.pl/xml/swd-platnik/1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="www.nfz.gov.pl/xml/swd-platnik/1" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:include schemaLocation="typy_danych.xsd"/>
    <xsd:attribute name="wersja">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="3"/>
                <xsd:minLength value="1"/>
                <xsd:enumeration value="1.0"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="info-aplik-nad">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="40"/>
                <xsd:minLength value="1"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="info-kontakt-nad">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="100"/>
                <xsd:minLength value="1"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="info-kontakt">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="100"/>
                <xsd:minLength value="1"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="umowa">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="24"/>
                <xsd:minLength value="1"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
</xsd:schema>

smpwc.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="www.csioz.gov.pl/xml/swd-platnik/1" xmlns:nfz="www.nfz.gov.pl/xml/swd-platnik/1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="www.csioz.gov.pl/xml/swd-platnik/1" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:include schemaLocation="typy_danych.xsd"/>
    <xsd:import namespace="www.nfz.gov.pl/xml/swd-platnik/1" schemaLocation="smp_nfz.xsd"/>
    <xsd:element name="komunikat">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="swiadczeniodawca"/>
                <xsd:element ref="zest-wyn-cyt"/>
            </xsd:sequence>
            <xsd:attribute name="typ" type="TTyp" use="required" fixed="SMPWC"/>
            <xsd:attribute name="wersja" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="TWersja">
                        <xsd:enumeration value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute ref="nfz:wersja" use="required"/>
            <xsd:attribute name="id-odb" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="TIdOdb">
                        <xsd:pattern value="[1-9]|1[0-6]"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="id-inst-odb" type="ID-INSTALACJI-NFZ" use="required" fixed="SIMP"/>
            <xsd:attribute name="id-nad" type="TIdOdb" use="required"/>
            <xsd:attribute name="id-inst-nad" type="ID-INSTALACJI" use="required"/>
            <xsd:attribute name="nr-gen" type="TNrGen" use="required"/>
            <xsd:attribute name="czas-gen" type="xsd:dateTime" use="required"/>
            <xsd:attribute ref="nfz:info-aplik-nad" use="optional"/>
            <xsd:attribute ref="nfz:info-kontakt-nad" use="optional"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="swiadczeniodawca">
        <xsd:complexType>
            <xsd:attribute name="typ-id-swd" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:length value="1"/>
                        <xsd:enumeration value="X"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="id-swd" type="TIdOdb" use="required"/>
            <xsd:attribute name="id-inst" type="ID-INSTALACJI" use="required"/>
            <xsd:attribute ref="nfz:info-kontakt" use="optional"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="zest-wyn-cyt">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="poz-umowy" maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute ref="nfz:umowa" use="required"/>
            <xsd:attribute name="rok" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="TRok">
                        <xsd:minInclusive value="2007"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="poz-umowy">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="badanie" maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="id-miej-wyk-swiad" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="6"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="nr-pkt-umowy" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="2"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="badanie">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="wyniki-bad"/>
                <xsd:element ref="personel-bad"/>
            </xsd:sequence>
            <xsd:attribute name="id-bad" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="10"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="pesel-pacj" type="PESEL" use="required"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="wyniki-bad">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="zalec-bad" minOccurs="0"/>
            </xsd:sequence>
            <xsd:attribute name="data-otrzym-prep" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:date">
                        <xsd:minInclusive value="2007-01-01"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="data-wyniku" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:date">
                        <xsd:minInclusive value="2007-01-01"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="nr-wyniku" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="20"/>
                        <xsd:minLength value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="wyniki-rozmazu" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="100"/>
                        <xsd:minLength value="1"/>
                        <xsd:pattern value="([A-C][0-9]{1,3})+"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="inne-kom-nowot" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="50"/>
                        <xsd:minLength value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="uwagi" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="200"/>
                        <xsd:minLength value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="zalec-bad">
        <xsd:complexType>
            <xsd:choice>
                <xsd:element ref="powt-bad"/>
                <xsd:element ref="dalsza-diagn"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="powt-bad">
        <xsd:complexType>
            <xsd:attribute name="liczba-mies" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="2"/>
                        <xsd:minInclusive value="1"/>
                        <xsd:maxInclusive value="12"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="lecz-pzapal" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="1"/>
                        <xsd:enumeration value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="proba-horm" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="1"/>
                        <xsd:enumeration value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="dalsza-diagn">
        <xsd:complexType>
            <xsd:attribute name="rodz-bad" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:positiveInteger">
                        <xsd:totalDigits value="1"/>
                        <xsd:minInclusive value="1"/>
                        <xsd:maxInclusive value="3"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="personel-bad">
        <xsd:complexType>
            <xsd:attribute name="pesel-cytot" type="PESEL" use="required"/>
            <xsd:attribute name="pesel-patom" type="PESEL" use="optional"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

typy_danych.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:simpleType name="TTyp">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="5"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TWersja">
        <xs:restriction base="xs:positiveInteger">
            <xs:totalDigits value="2"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TIdOdb">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="16"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ID-INSTALACJI-NFZ">
        <xs:restriction base="xs:string">
            <xs:minLength value="0"/>
            <xs:maxLength value="38"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ID-INSTALACJI">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="38"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TNrGen">
        <xs:restriction base="xs:positiveInteger">
            <xs:totalDigits value="8"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TRok">
        <xs:restriction base="xs:gYear">
            <xs:minInclusive value="1850"/>
            <xs:maxInclusive value="2199"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="PESEL">
        <xs:restriction base="xs:string">
            <xs:length value="11"/>
            <xs:pattern value="\d{11}"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TZeroJeden">
        <xs:restriction base="xs:nonNegativeInteger">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="1"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="TZeroJedenDwa">
        <xs:restriction base="xs:nonNegativeInteger">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="2"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>


Solution 1:[1]

I tried to reproduce your error. And in some way, I have reproduced it. But after changing your XML - removing the "-here-is-error-" - to

<badanie id-bad="9536768" pesel-pacj="82020477523">
    <wyniki-bad data-otrzym-prep="2021-07-23" data-wyniku="2021-07-27" nr-wyniku="00000/21/G" wyniki-rozmazu="A20B1B3C254"/>
    <personel-bad pesel-cytot="72081092874"/>
</badanie>

the XML validates just fine. Take care that the limit of the attribute "id-bad" is 10 chars (here this condition is satisfied).

So the cause of your problem must be somewhere else, and I guess it is the XSD file you're using. It only works if you validate against smpwc.xsd, because this is the file that includes the other XSDs.

So you probably should change your Delphi line (I'm not fluent in Delphi, but this should only indicate the way to go.) to validate against smpwc.xsd and not typy_danych.xsd:

Begin
  XmlDoc := codomdocument60.create;
  Xmldoc.async := False;
  Xmldoc.load('file-to-test.xml');

  Schemadoc := codomdocument60.create;
  Schemadoc.async := False;
  Schemadoc.load('smpwc.xsd');
...

Then, the other XSD files should be included automatically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zx485