In Delphi - MSXML parser not validating on load

Hedgehog
Hedgehog used Ask the Experts™
on
I'm new to all this, but I have found your question "Title: MSXML parser not validating on load" in the MFC section and I have the same problem in Delphi.  I have the following Delphi code:
------------------
unit msxmlvalidation;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,
  MSXML2_TLB;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure formload(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

var
    doc: IXMLDOMDocument;
   
begin
   
    doc := CoDOMDocument.Create;
    doc.async := False;
    doc.validateOnParse := true;
    doc.resolveExternals := true;
    if doc.load (edit1.text) then
    begin
        showmessage ('File is valid - Whoopee!');
    end
    else
        //error loading
        ShowMessage ('Error in XML file');
   
end;




procedure TForm1.formload(Sender: TObject);
begin
     edit1.text := 'C:\invalid.xml' ;
end;

end.
----------------

and the following lines in the top of my XML file:-

------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--Document created automatically -->
<Specification TimeZone="GMT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\XML\XSDs\Export.Xsd">
-----------------------

I am using Delphi version 5 and MSXML v4.0 (just downloaded last week).  I used your info about validateonparse and resolveexternals and there was a comment about xsi:noNamespaceSchemaLocation only being added in MSXML v4 but I am using v4 and it still doesn't work.  

Any help gratefully received.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

you forget to close the tag Specification. Change your xml header to this below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--Document created automatically -->
<Specification TimeZone="GMT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\XML\XSDs\Export.Xsd"/>

or
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--Document created automatically -->
<Specification TimeZone="GMT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\XML\XSDs\Export.Xsd"></Specification>

Regards,

Douglas.

Author

Commented:
Thanks for the above comment but it is not the answer.  The snippet was only the top of my xml file - I had a </Specification> later on in my document.  I have just found out from elsewhere that MSXML does "lazy" parsing on the LOAD function and to do full validation you need to use the VALIDATE function.  I am currently trying this but getting error 1072897500 "validate failed because the root element has no associated DTD/Schema" - it could be an error in my XML (although it validates in XML-SPY).  I'll let you know the outcome.
Could you post the whole XML file here?

Douglas
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
The XSDfile C:\XML\XSDs\Jane.Xsd is as follows:
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ns="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>
     <xs:element name="Product">
          <xs:complexType>
               <xs:sequence>
                    <xs:element ref="Core"/>
               </xs:sequence>
               <xs:attribute name="Language" type="xs:string" use="required"/>
               <xs:attribute name="Country" type="xs:string" use="required"/>
          </xs:complexType>
     </xs:element>
     <xs:element name="Specification">
          <xs:complexType>
               <xs:sequence>
                    <xs:element ref="Product" maxOccurs="unbounded"/>
               </xs:sequence>
               <xs:attribute name="TimeZone" type="xs:string" use="required"/>
          </xs:complexType>
     </xs:element>
     <xs:element name="Core">
          <xs:complexType>
               <xs:choice maxOccurs="unbounded">
                    <xs:element ref="ProductId"/>
               </xs:choice>
          </xs:complexType>
     </xs:element>
     <xs:element name="ProductId">
          <xs:simpleType>
               <xs:restriction base="xs:string">
                    <xs:enumeration value="A"/>
               </xs:restriction>
          </xs:simpleType>
     </xs:element>
</xs:schema>
--------------------

and the XML file is here:
------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Specification TimeZone="GMT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\XML\XSDs\Jane.Xsd">
     <Product Language="EN" Country="ZE">
          <Core>
               <ProductId>A</ProductId>
          </Core>
     </Product>
</Specification>
-----------------------

I am still getting the same "Validate failed ..." message as before.  Someone suggested that I should try using the SAX parser instead of the DOM parser but I'm not sure why?

Author

Commented:
I am now getting the message "Incorrect definition for the root element in schema" when I try to ADD the xsd file to the schema cache before validating.  Searching around this looks like it may be because my xml file uses the specification laid down in the 2000 spec (which XMLSPY uses which is why it validates ok in XMLSPY) of XML whereas MSXML uses the 2001 spec. I'm still not sure what changes I have to make yet but will continue trying.

Author

Commented:
I have now tried to .add an xsd to the schema cache using the sample xml and xsd files from MSXML v4.0 Core services help and I still get the error "Incorrect definition for the root element in schema" - any help what am I doing wrong?
Hi Hedgehog,

I've tested your XML and XSD files with your code and it doesn't show any error... Maybe the problem is about your XML Parser version.

regards, Douglas
Hi Hedgehog,

try to install the version you've download again... I've downloaded the version 4 and the xml was validated succesfully.

regards, Douglas

Author

Commented:
Hi

I'm now running it on another machine and have downloaded MSXML 4 again and it does load ok and it's supposed to validate against the schema when it loads but mine is not validating against the schema it always says it is ok on load but if I give it some incorrect data in the xml it still thinks its ok.  Are you just using the LOAD function or are you doing the VALIDATE as well ?  Have you tried it with invalid data in your XML ?  The LOAD does seem to pick up any XML syntax errors but not errors where the XML doesn't match the XSD.

Thanks for your response so far
Commented:
All working now!

My code needed to use CoDomDocument40 and CoXMLSchemaCache40 rather than just CoDomDocument and CoXMLSchemaCache. I wrongly assumed that as I imported the MSXML version 4 type library that the functions etc would all be version 4 but I was mistaken.  I also had a problem with the schema being non-deterministic as it had a sequence with elements appearing more than once but once these were removed it was ok.  XMLSPY and MSXML must be  using different specs of XML.

Author

Commented:
All working now!

My code needed to use CoDomDocument40 and CoXMLSchemaCache40 rather than just CoDomDocument and CoXMLSchemaCache. I wrongly assumed that as I imported the MSXML version 4 type library that the functions etc would all be version 4 but I was mistaken.  I also had a problem with the schema being non-deterministic as it had a sequence with elements appearing more than once but once these were removed it was ok.  XMLSPY and MSXML must be  using different specs of XML.

Hedgehog:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial