簡體   English   中英

使用 delphi 根據模式驗證 XML

[英]Validate XML against schema with delphi

大家見鬼去吧

我正在嘗試使用 delphi 過程針對模式驗證 XML,但我遇到了類似這樣的錯誤:節點不正確,也不正確 - 找不到 DTD 定義。

我不確定我是否為程序提供了模式文件的正確順序,也不確定程序是否正確。

我希望任何人都可以幫助我。

這是我的 delphi 程序

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 基於 Microsoft XML v6.0。

這是我的 XML,其中包含我想用我的程序檢測到的錯誤。 錯誤是我放置“-here-is-error-”的位置。

由於數據保護原因,患者/醫生的 ID 是隨機的,但從架構的角度來看 - 是正確的。

<?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>

我得到了三個文件,可以讓我驗證 XML:smp_nfz.xsd、smpwc.xsd、typy_danych.xsd。

他們在這里

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>

我試圖重現你的錯誤。 我以某種方式復制了它。 但是在更改您的 XML 之后 - 刪除“-here-is-error-” - 到

<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>

XML 驗證正常 注意屬性“id-bad”的限制為 10 個字符(此處滿足此條件)。

所以你的問題的原因一定是在別的地方,我猜是你正在使用的 XSD 文件。 它僅在您針對smpwc.xsd進行驗證時才有效,因為這是包含其他 XSD 的文件。

因此,您可能應該更改您的 Delphi 行(我不熟悉 Delphi,但這應該只指示通往 go 的方式。)以針對smpwc.xsd而不是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');
...

然后,應該自動包含其他 XSD 個文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM