1
votes

I'm working on creating XSD files for various XML documents, I'm currently using the XML Tools Notepad++ plugin, and when I try to validate my XSD files, it asks for XML Schema to validate against: I'm not sure how to properly point XML Tools to the right schema to validate my XSD file, since the schema for an XSD file is not locally defined, its defined universally by W3. This is my current schema tag declaration:

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3.org/2009/XMLSchema/XMLSchema.xsd">

I've tried looking up a solution on other sites, but I haven't exactly found a solution. This is my full XSD file as of now. I'm thinking that XML Tools either can't resolve the location of a non-local URI or XML Tools isn't properly recognizing my XSD file as an XML Schema, but I'm not entirely sure. It's also possible that I'm simply not declaring something properly.

Just to be clear, I'm not at the point of validating my XML file, I'm simply trying to create an XSD that is properly formed.

2
What is the exact issue? Do you mean to say that you want to validate xml against xsd? But the summary says creation xsd. Please clarify by updating the question. - Rao
@Rao I've updated the question a bit, hopefully it should be more clear. - kavatika

2 Answers

2
votes

The best way to validate that a schema is correct is to submit it to a schema processor. This will do a much more thorough check than merely assessing it against the schema-for-schema-documents (S4SD). There are many constraints on a schema that cannot be expressed in the S4SD.

In fact these are of two kinds.

(1) Schema representation constraints are constraints at the level of the schema document, for example (in XSD 1.1 part 1 §3.9.1):

In addition to the conditions imposed on <any> and <anyAttribute> element information items by the schema for schema documents, namespace and notNamespace attributes must not both be present.

(2) Schema component constraints are expressed in terms of constraints on the schema component model generated from the source schema document. An example is the "element declarations consistent" constraint (§3.8.6.3) which says that if two element particles in a content model have the same name then they must also have the same type.

Different schema processors will provide different ways of validating a schema. For example with Saxon on .NET you can just do

Validate -xsd:my-schema.xsd -t

(supplying a schema and no source document) and it will tell you if there are any errors in your schema.

1
votes

If I am correct, you are attempting to validate your XML Schema against the Schema for Schemas.

The target namespace must be that of XML Schema, i.e., the same as the namespace bound to the prefix xs:, and the location of the file containing the Schema for Schemas can be specified with the attribute xsi:schemaLocation.

<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="https://www.w3.org/2001/XMLSchema"
  xsi:schemaLocation="https://www.w3.org/2001/XMLSchema
                      https://www.w3.org/2009/XMLSchema/XMLSchema.xsd">

More generally, the xsi:schemaLocation attribute contains an even number of space-separated URIs. Each URI at an odd position is a namespace, and the URI at the next even position is its location hint.

Note that there a high chance, but no 100% guarantee that this works, because the XML Schema specification defines the information in xsi:schemaLocation as a hint that the schema validation engine may or may not follow to resolve its schema. A validation engine is free to provide a different way to resolve schema file locations, which should then be documented. Having said that, if an engine does not have a schema in its cache, it is very likely that it will use the hint provided with xsi:schemaLocation as this is widely established practice.

In general, validation engines are encouraged to cache schemas (either builtin or one-time-download), as schema locations pointed to a central server by a worldwide user base can put a significant load on the servers, especially for the W3C. Having the schema locally also diminishes the latency. If the engine does not cache and the validation is run often, a possibility is to download the Schemas for Schemas and use it locally.

Finally, an XML Schema engine will normally automatically validate and check schemas when used, not only against the Schemas for Schemas but also considering all further constraints in the specification. If such is the case, all of the above happens automatically without the need to specify the schema location inside a schema. Doing so explicitly is a nice intellectual exercise, though.