Question

I get doubt that are there some schemas which have a valid schema but don't have some XML documents? If there are, could you please give me some examples?

Was it helpful?

Solution

Yes, it's possible to define schemas for which the set of valid documents is the empty set -- at least, in every schema language I know, and given a reasonable definition of "valid document".

In XSD, RNG, and DTDs, perhaps the simplest such schema is one which declares no elements. In XSD, this could be expressed this way:

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

A simple non-vacuous schema can be unsatisfiable by declaring elements with unsatisfiable types:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="unsatisfiable">
    <xs:complexType>
      <xs:choice/>
    </xs:complexType>
  </xs:element>
</xs:schema> 

Since xs:choice requires that at least one child of the choice be matched by the input, a choice with no children is unsatisfiable. And if the choice is required, as it is here, then the type as a whole is unsatisfiable.

Empty choices can also be used in Relax NG, though not in DTDs.

In Relax NG, it's also possible to declare satisfiable elements in a schema with no valid instances, as long as the root element or at least one required descendant of the root element is unsatisfiable. In XSD, by contrast, once you have any satisfiable element declarations or type definitions you no longer have an empty language: XSD provides no way to say, in the schema, what the outermost element must be at validation time.

In XSD, RNG, and DTDs it is also possible to make an element unsatisfiable by requiring that it contain undeclared elements. In DTD notation:

<!ELEMENT unsatisfiable (undeclared) >

Also, in any of these languages, it's possible to define schemas which are satisfiable only by infinite documents:

<!ELEMENT e (e) >

In XSD (and in Relax NG using XSD datatypes) it's possible to define empty simple types, too:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="unsatisfiable">
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minExclusive value="0"/>
        <xs:maxExclusive value="1"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema> 

Some methods of defining empty types are forbidden by XSD: setting the minimum and maximum exclusive values to the same value, for example, will raise an XSD error. (The rationale here is that the majority in the WG thought that an empty type made no sense and also suffered from the illusion that they could effectively prevent the definition of empty types, at least in cases not involving regular-expression patterns. As the example shows, they were wrong.) In XSD 1.1, perhaps the cleanest and most obvious way to define a non-satisfiable simple type is to define an empty union type, but an even simpler way is to use the predefined xs:error type (which itself is defined as an empty union). This is not possible in XSD 1.0, which requires that unions have at least two member types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top