Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation of XML using schema (.XSD file)

Status
Not open for further replies.

Kenbla

Programmer
Oct 11, 2006
68
SE
I have a problem with the validation of XML code using a schemafile (.XSD file). I have, on purpose, entered some bad code in the .XSD file which should result in validation errors but when I load (and validate) my XML file it passes without problems!!!
Is there someone out there who has any experience of such a problem?
I have tried to use a number of different XML properties (such as: Async, ValidateOnParse etc.) and methods but I still have problems.

Any assistance is greatly appreciated!
/Kenbla
 
I'm confused... why would you put bad code in the schema file? In production, that file lives on your server for other people to validate their XML files against.
 
The reason I want to enter bad code in my schemafile is that I want to test the validation functionality!! When I know that it works OK, I will of course remove my bad code!
 
I tend to agree with Miros. It would seem that you would want to test known invalid XML documents against the valid schema, as well as test multiple valid documents. I am not sure you gain much information testing valid documents against a different schema.

Unfortunately for this particular problem, I am not much of an expert on schema validation. It is my observation that, at least in my experience, XML schema are far more frequently used with development tools during the development process, and schema validation is of little use in production environments unless your documents are coming from untrusted sources.

Tom Morrison
 
>I have, on purpose, entered some bad code in the .XSD file which should result in validation errors but when I load (and validate) my XML file it passes without problems!!!
It is possible that the "bad" is immaterial to validation. If the bad is such that xsd is no longer a well-formed xml document, and that the same phenomenon happens, it is then likely validation has never taken place. The "bad" is per degree. Even xsd is still a well-formed, if validation takes places properly, the result would be invalidating the xml document with sometimes misleading error source.

In any case, make sure validation actually take place.

 
My schemafile MUST be validated during execution of my program!! as it is from an outside vendor, and I would appreciate any tips or tricks to make my validation work!
The XML file and schema file must "match" and both files may contain errors and must therefore be validated.

I load my schema (.XSD file) into a schemaobject which is then "connected" to the XML object. Validation should take place when I load my XML file to the XML object BUT for some odd reason that's not true in this particular case. Has anyone of you experienced similar problems?
 
Make sure you have the line
[tt].add "","<your xsd file>"[/tt]
with the xmlschema object as receiver.
 
Thanks for your reply. Yes I have that line of code in my program, and I have validated XML in other programs earlier without any problems.

This is what my code looks like (Delphi program!):

oXMLSchema := CoXMLSchemaCache40.Create;
oXMLSchema.add('xsi:noNamespaceSchemaLocation', sPath + 'TempXSD.xsd');
oXMLObject := CoDOMDocument40.Create;
oXMLObject.schemas := oXMLSchema;
oXMLObject.preserveWhiteSpace := True;
oXMLObject.load(sTempPath + 'TempXML.xml');
if oXMLObject.parseError.errorCode <> 0 then
begin
Validation error, message to user
end;
 
Pay enough attention to namespace as defined in the xml doc. You have to add to it as well, such as.
[tt].add('urn:library',sPath + 'TempXSD.xsd');[/tt]
if there is a namespace "urn:libaray", or alike - you get the idea, in the xml.
 
There is no such thing in my XML file!! so there's got to be something else!!
 
Wild idea. Try opening the schema file as if it were just an XML file. That should tell you if your schema file is valid, and give you an error if it isn't. If it's not valid, obviously, don't try validating documents against it.

I still don't see why the schema file is so fluid. Your vendor should pick one schema and stick to it! Otherwise, both of you are shooting at moving targets. Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top