2

This is slightly different from just Schema validation. I'm asking how in C# you can check that not only is the document valid against a schema, but also validate that the schema actually applies to that document. I'd prefer a .NET / C# answer but any answer that fully respects the document standards will suffice.

Daniel Green
  • 634
  • 5
  • 14
  • You can find the answer [**here**](http://stackoverflow.com/questions/124865/xml-schema-xsd-validation-tool?rq=1) – Petros Tsialiamanis May 26 '13 at 04:24
  • problem is most of these consider a document valid if you have an xml document that specifies no schema, or a different schema than you have to validate it against. – Daniel Green May 26 '13 at 04:31

1 Answers1

2

I am making some assumptions as to what exactly you're looking for.

Having said "a specific schema" to me it means that you have a schema and that you're sifting through XML files trying to understand first if that schema should even be used to validate an XML.

First, I would lay some background... A "schema" could be that which one gets in a single file, or spread across multiple files. With multiple files, there are a couple of relationships possible between the XSD files: include, import, redefine; then there's the including of a schema file without a target namespace, by a schema file with a target namespace (this is typically called chameleon). So instead of "schema" I would prefer to use the term "schema set".

Some things to consider then:

  • A chameleon XSD in your "schema set" may not be intended to validate an XML having an unqualified document element.

  • A redefined XSD should not be used to validate matching XML content; the redefining XSD should.

  • Even though an XSD defines abc as a global element, it may not be acceptable to process XML instances that feature abc as the root element.

The above is to indicate that even though an XML may appear to implement a "specific schema", in itself it doesn't mean it matches the intent the author of the XSD placed in that schema.

Considering the above logic defined and implemented somehow, the verification I would do, as an answer to your question, would be to find the XSD definition of a non-abstract, global element - an XmlSchemaElement - in a specific XmlSchemaSet, using the full qualified name of the root element in the XML I am verifying.

System.Xml.Schema.XmlSchemaSet xset = ...; // Loaded somehow
System.Xml.XmlQualifiedName qn = ...; // LocalName + NamespaceURI
if (xset.GlobalElements.Contains(qn))
{
    System.Xml.Schema.XmlSchemaElement el = (System.Xml.Schema.XmlSchemaElement)xset.GlobalElements[qn];
    if (!el.IsAbstract)
    {
        // The XML file may implement the schemata loaded in this schema set.
    }
}

I would expect this to at least help you improve your question, if I am off.

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Taking some time to explore and absorb but it certainly brings me in a positive direction thank you :D – Daniel Green May 26 '13 at 20:17
  • I also submitted my source and documents here. http://stackoverflow.com/questions/16755058/validating-xml-documents-with-xsd-correctly – Daniel Green May 26 '13 at 23:54