procedure TImportExport.CheckXMLByXSD(xml_file, xsd_file: string; out FileName: string);
var
FXMLDocument: IXMLDOMDocument2;
FXMLDOMSchema: IXMLDOMSchemaCollection2;
FXMLParserError: IXMLDOMParseError2;
s: string;
i: integer;
flk: IXMLFLK_PType;
begin
FileName:= '';
try
FXMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0') as IXMLDomDocument2;
FXMLDOMSchema := CreateOleObject('Msxml2.XMLSchemaCache.6.0') as IXMLDOMSchemaCollection2;
FXMLDOMSchema.add('', xsd_file);
FXMLDocument.Async := false;
FXMLDocument.resolveExternals:= false;
FXMLDocument.validateOnParse := false;
FXMLDocument.setProperty('MultipleErrorMessages', true);
FXMLDocument.load(xml_file);
FXMLDocument.schemas := FXMLDOMSchema;
FXMLParserError := FXMLDocument.validate as IXMLDOMParseError2;
finally
if (FXMLParserError.errorCode <> 0) then
begin
s:= '';
flk:= NewFLK_P;
flk.FNAME:= ReplaceFirstChar(ExtractFileName(xml_file),'V');
flk.FNAME_I:= ExtractFileName(xml_file);
with FXMLParserError.allErrors do
for i:= 0 to Length - 1 do
begin
with flk.PR.Add do
begin
case Item[i].ErrorCode of
-1072897535: OSHIB:= 903;
-1072898028: OSHIB:= 902;
end;
BAS_EL:= Item[i].errorXPath;
COMMENT:= StringReplace(Item[i].reason, #13#10, '', [rfReplaceAll]);
end;
s:= s + Format('ErrorCode: %d' + #13#10 + 'Reason: %s' + #13#10 +
'SrcText: %s' + #13#10 + 'Line: %d' + #13#10 +
'LinePos: %d' + #13#10 + 'FilePos: %d' + #13#10 +
'XPath: %s', [Item[i].ErrorCode, Item[i].reason, Item[i].Srctext, Item[i].Line, Item[i].LinePos, Item[i].FilePos, Item[i].errorXPath])+ #13#10;
end;
FileName:= TempFolder + ReplaceFirstChar(ExtractFileName(xml_file),'V');
flk.OwnerDocument.LoadFromXML(XMLDoc.FormatXMLData(flk.OwnerDocument.XML.Text));
flk.OwnerDocument.SaveToFile(FileName);
raise Exception.Create('Файл ' + ExtractFileName(xml_file) +
' не соответствует своей XSD схеме ' + ExtractFileName(xsd_file) + #13#10 + s);
end;
FXMLParserError:= nil;
FXMLDOMSchema:= nil;
FXMLDocument:= nil;
end;
end;
from project 🙂