[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Validating documents against XML Schema with different namespace using M

  • To: "XML Dev List (xml-dev@l...)" <xml-dev@l...>
  • Subject: Validating documents against XML Schema with different namespace using MSXML
  • From: Morten Grøtan <Morten.Grotan@e...>
  • Date: Mon, 30 Sep 2002 10:29:17 +0200

xmlschemacache default namespace
Title: Validating documents against XML Schema with different namespace using MSXML

Hi,
I've only just started using XML Schemas with the MSXML parser (4.0 SP1), and have trouble grasping a few baseline things.

The real-life task I set out to solve with XML Schemas was to be able to validate documents prior to importing to a production database. The documents are received as part of an automatic collector service, hence I don't have 100% control of what kind of files are received. I want to be able to only import 100% valid documents, according to my schema, and avoid having tons of custom validation logic within my import program.

Making the schema itself for a given instance of a typical valid document has proved to be no problem, and I am also able to enforce the constraints specified in my schema. So far so good. But what happens is that I purposely place an invalid document among the other valid ones, to see what happens.

The real problem, I've found, is in the use of namespaces (what a surprise, having read lots of different articles about namespaces lately....). I have defined both a default and a target namespace in my schema and everything works just fine as long as I have the same namespace in my instance document. But as soon as there is the slightest mismatch in namespaces, the document is NOT validated against the schema, hence rendering my validation mechanism utterly useless.

I've sketched out 4 scenarios below, using the following terminology:
xsd: An XML Schema
xml: An XML instance document
xmlns: Default namespace
targetns: XML Schema target namespace

Scenario 1:
xsd: no xmlns and no targetns
xml: no xmlns
uses xsd and correctly says invalid xml is invalid

Scenario 2:
xsd: no xmlns and no targetns
xml: xmlns set to something else than targetns
does NOT use xsd and incorrectly says invalid xml is valid

Scenario 3:
xsd: xmlns and targetns
xml: xmlns
uses xsd and correctly says invalid xml is invalid

Scenario 4:
xsd: xmlns and targetns
xml: no xmlns or xmlns set to something else than targetns
does NOT use xsd and incorrectly says invalid xml is valid

The way I've implemented this is using the MSXML2.XMLSchemaCache.4.0 object to add a schema, associating it with the namespace defined in the schema, and then setting the "schemas" property of the XMLDom object (MSXML2.DOMDocument.4.0) to point at the XMLSchemaCache object. Then I just call the load method of the XMLDom object, and check the parseError object of the XMLDom object for any errors.

So, why does this happen? Shouldn't MSXML be able to find out that my instance document does not share the same namespace as my schema, hence invalidating the document? What is the purpose of the whole XML Schema shebang elsewise? If you can't be sure that invalid documents are in fact invalidated, without writing tons of validation code yourself, why even bother using a schema? Needless to say, I'm slightly disillusioned right now, and possibly soon will be having a problem with my boss, trying to explain why I've wasted so much time on this schema thing without producing a useful validation mechanism.....

As for the XMlSchemaCache object, I'm aware that you can add several schemas into it before attaching it to the XMLDom object, hence using all the schemas to validate the instance document. Admittedly it can seem hard to see which (if any) schema should be responsible for invalidating the document, but in my opinion ANY of them should be able to do that, as long as they can't be validated against the document. So, if at least one of the added schemas fail, the whole validation should fail. Or am I totally naïve?

Mvh / Kind regards,
Morten Grøtan
morten.grotan@e...
Utvikler, MCSD

Electric Farm ASA, Rosenkrantz gt. 21, NO-0160 OSLO, Norway, http://www.electricfarm.no/
mob: +47 92 88 59 72, tel: +47 48 00 09 99, fax: +47 23 10 30 10


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.