Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Invalid XML gets through MSXML validation

Tags:

xml

delphi

msxml

I've tried to revrite a JScript example at MSDN in order to validate XML against certain schemas.

As a first attmempt, I've used the sl-valid.xml, sl-notValid.xml, and sl.xsd files as used in the example.

My code goes as follows:

procedure BasicValidation(FileName: string);
var
  XML: IXMLDOMDocument2;
begin
  // Load XML and resolve externals
  XML := ComsDOMDocument.Create;
  XML.async := False;
  XML.validateOnParse := True;
  XML.resolveExternals := True;
  XML.setProperty('SelectionLanguage', 'XPath');
  XML.setProperty('SelectionNamespaces', 'xmlns:x=''urn:book''');
  XML.load(FileName);
  if XML.parseError.errorCode <> 0 then
    ShowMessage('Error parsing. Reason: ' + XML.parseError.reason)
  else
    ShowMessage('XML validation OK.');
end;

When I try the sl-notValid.xml file, I still get 'XML validation OK'. Have anyone seen this before? What's the fundamental difference between the above code and that of the JScript examle http://msdn.microsoft.com/en-us/library/ms764717%28VS.85%29.aspx ?

like image 994
conciliator Avatar asked Sep 30 '09 18:09

conciliator


1 Answers

try this

procedure BasicValidation(FileName: string);
var
  XML: IXMLDOMDocument2;
begin
  XML := CoDOMDocument40.Create;
  XML.async := False;
  XML.validateOnParse := True;
  XML.resolveExternals := True;
  XML.setProperty('SelectionLanguage', 'XPath');
  XML.setProperty('SelectionNamespaces', 'xmlns:x=''urn:book''');
  XML.load(FileName);
  if XML.parseError.errorCode <> 0 then
    ShowMessage('Error parsing. Reason: ' + XML.parseError.reason)
  else
    ShowMessage('XML validation OK.');
end;

Explanation, you must explicitly call a constructor of a version that support for XSD schema validation (MSXML>= 4).

Bye.

like image 191
RRUZ Avatar answered Sep 26 '22 23:09

RRUZ