Re: Equivalent of DOCTYPE in Schemas?

Hi Nigel,

> When using a DTD, although all <!ELEMENT>s could potentially be the
> root element of a document, the <!DOCTYPE> declaration in the
> instance document governs which element will be the root element.
>
> So my question is, how can this be done with Schemas? Again,
> potentially any top level element declared in a Schema could be a
> valid root element in an instance document. How is the root element
> specified? xsi:SchemaLocation...doesn't seem to have any mechanism
> for specifying the root element name....

You're absolutely right that when you use a DOCTYPE declaration in an
XML document, you have to specify the name of the document element in
the DOCTYPE declaration. So:

<!DOCTYPE foo SYSTEM 'foo.dtd'>
<foo />

is valid whereas:

<!DOCTYPE foo SYSTEM 'foo.dtd'>
<bar />

is not. However, there is nothing in the *DTD* that says that a
particular element has to be the document element in the instance. If
you have foo.dtd, which declares foo and bar elements, then either of
these is valid:

<!DOCTYPE foo SYSTEM 'foo.dtd'>
<foo />

<!DOCTYPE bar SYSTEM 'foo.dtd'>
<bar />

All the DOCTYPE declaration actually does is say 'this is the document
element this document uses', which is pretty useless because you can
find that out by just looking at the document element!

So yes, there is the equivalent of the name of the document element in
the DOCTYPE declaration which you can use in XML Schema, and that's
the name of the document element :) The schema validator starts there
when it starts validating.

I think what you're really asking is whether you can constrain, within
an XML Schema, which element is the document element. In general, the
answer is no, because you can have several element declarations at the
top level of your schema and there's nothing stopping an instance
document from being validated against any of those global element
declarations.

However, you can design your XML Schema so that there is only one
global element declaration, with all of the rest of the element
declarations hidden within complex type definitions or model groups.
If you do this, the only element that an instance document could have
as its document element and still be valid would be the element that
you've declared globally. For example:

<xs:element name="foo" type="fooType" />

<xs:complexType name="fooType">
  <xs:sequence>
    <xs:element name="bar" />
  </xs:sequence>
</xs:complexType>

could only be used to validate instance documents whose document
element was a foo element.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Wednesday, 12 December 2001 13:37:38 UTC