XSD: XML files pass validation, but so do XSLs and XSDs

后端 未结 4 491
孤独总比滥情好
孤独总比滥情好 2020-12-12 05:59

So there\'s an XSD schema that validates a data file. It declares root element of the document, and then go complexType\'s that describe structure. The schema h

相关标签:
4条回答
  • 2020-12-12 06:36

    Right.

    It turned out, validation has three possible results, not two -- valid, invalid and unknown. So Boolean return value of CheckValidity function is somewhat surprising.

    If the root node of the document is not described by the schema, the document passes validation without errors, and no validation events occur, but the root node receives "unknown" status. This, for our purpose, is a fail. So we also need to check the XMLNode.SchemaInfo.Validity member of the root node.

    I wish Validate() method documentation was a bit clearer on that.

    0 讨论(0)
  • 2020-12-12 06:42

    My understanding is that XML Schema (XSD) does not give any way of requiring that the root node of a document is a certain element -- the only way to do that is to restrict what elements are defined at "global level" to just one element. Is it possible that your validation code is importing the schema for XSLT, so that when it sees an XSLT document it validates because the XSLT elements have been defined at global level.

    0 讨论(0)
  • 2020-12-12 06:43

    XML schemas really validate elements within a namespace, not documents. There's no XML Schema rule that says that the top-level element of the instance document must be within a specific namespace. This fits in with the general idea that a namespace is its own little world, and it prevents me from writing a schema in my namespace that will invalidate documents in yours. If an element's not in my namespace, it's none of my business

    This means that when validating instance documents, you have to check to make sure that the top-level element of the document you're validating is in a namespace that your application accepts - which, in your application, is simply the default namespace.

    0 讨论(0)
  • 2020-12-12 06:52

    Without having seen any code, I'm going to take a stab and suggest that it just may be because your validation is setting the ValidationType on the XmlReaderSettings object, but you're either not wiring up the ValidationEventHandler to check for validation errors or simply not doing anything with these validation events.

    Even with XmlDocument.Validate, you need to wire up this ValidationEventHandler.

    See MSDN here.

    0 讨论(0)
提交回复
热议问题