1
  private
    FSchemaFileName: string;
    FXmlFileName: string;
  end;

uses
  XMLDoc, XMLIntf, XMLValidate;

resourcestring
  RsValidateOk = 'Document validated without errors.';

{$R *.dfm}

procedure TMainForm.FormShow(Sender: TObject);
begin
  FontEdit1Accept(nil);
end;

procedure TMainForm.XmlFileOpen1BeforeExecute(Sender: TObject);
begin
  XmlFileOpen1.Dialog.FileName := '';
end;

procedure TMainForm.SchemaFileOpen1BeforeExecute(Sender: TObject);
begin
  SchemaFileOpen1.Dialog.FileName := '';
end;

procedure TMainForm.XmlFileOpen1Accept(Sender: TObject);
begin
  FXmlFileName := XmlFileOpen1.Dialog.FileName;
  XmlFileEdit.Lines.LoadFromFile(FXmlFileName);
  PageControl.ActivePageIndex := 0;
end;

procedure TMainForm.SchemaFileOpen1Accept(Sender: TObject);
begin
  FSchemaFileName := SchemaFileOpen1.Dialog.FileName;
  SchemaEdit.Lines.LoadFromFile(FSchemaFileName);
  PageControl.ActivePageIndex := 1;
end;

procedure TMainForm.FontEdit1Accept(Sender: TObject);
begin
  XmlFileEdit.Font := FontEdit1.Dialog.Font;
  SchemaEdit.Font := FontEdit1.Dialog.Font;
end;

procedure TMainForm.FormatXmlAction1Update(Sender: TObject);
begin
  FormatXmlAction1.Enabled := ActiveControl is TRichEdit;
end;

procedure TMainForm.FormatXmlAction1Execute(Sender: TObject);
begin
  with ActiveControl as TRichEdit do
    Lines.Text := FormatXMLData(Lines.Text);
end;

procedure TMainForm.ValidateContent1Execute(Sender: TObject);
var
  Doc: IXMLDocument;
begin
  Doc := LoadXMLData(XmlFileEdit.Lines.Text);
  ValidateXMLDoc(Doc, FSchemaFileName, '');
  Application.MessageBox(PChar(RsValidateOk), PChar(Caption), MB_OK or MB_ICONINFORMATION);
end;

procedure TMainForm.ValidateContent1Update(Sender: TObject);
begin
  ValidateContent1.Enabled := (XmlFileEdit.GetTextLen > 0) and FileExists(FSchemaFileName);
end;

Gives me "It is an error to mix objects from different versions of MSXML" on "MsxmlDoc.schemas := SchemaCache;" Fallowing code can be downloaded at Embarcadero http://cc.embarcadero.com/Item/19688

This is XSD I use https://www.swedbank.lt/files/PDF/Swedbank_e-invoice_lt_1.1.xsd and this is XML I use just to test things https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx

How do I validate XML against XSD?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Edijs Kolesnikovičs
  • 1,627
  • 3
  • 18
  • 34
  • Have you looked at http://stackoverflow.com/questions/30654095/validate-xml-using-windows-dom-and-txmldocument-doesnt-work-on-some-computers and http://stackoverflow.com/questions/446635/schema-validation-with-msxml-in-delphi ? – RBA Sep 06 '16 at 06:13
  • There is no `MsxmlDoc.schemas := SchemaCache;` statement in the code shown. Please don't reference code on external sites. StackOverflow questions are meant to be self-contained. Please [edit](http://stackoverflow.com/posts/39340608/edit) the question to include the relevant code that is actually causing the error. – Remy Lebeau Sep 06 '16 at 15:48

1 Answers1

1

This is not an XSD/XML problem but rather a problem with how MSXML is being used:

See PRB: Mixing Different Versions of MSXML DOM Objects Is Not Recommended

Cause

When you mix different versions of MSXML DOM objects in a DOM object's method call, the object from the differing version of the parser that is supplied as a required method parameter is treated as a foreign object.

Resolution

Reference and use objects implemented by a single version of the MSXML parser. Do not mix different versions of DOM Objects when you program the MSXML DOM.

kjhughes
  • 106,133
  • 27
  • 181
  • 240