XML.com 
 Published on XML.com http://www.xml.com/pub/a/1999/07/schemas/validity.html
See this if you're having trouble printing code examples

 

Validity
By Norman Walsh
July 01, 1999

Saying that a document is "valid" means that it fits within the described model of a class of documents. There are many reasons why you might want to make sure your documents are valid:

Using a schema and a validating parser offers one standard way to test your documents. (Valid documents can still be semantically wrong: you can submit a purchase order that asks for a hundred boxes of staples when you meant to ask for ten, but checking validity catches a lot of "obvious" errors.)

Every document that you encounter can be defined in one of four ways:

  1. If it is not well-formed, it isn't XML.

  2. If an XML document does not identify a schema to which it claims to conform (and no schema can be inferred), then it is simply well-formed.

  3. If a schema is (or can be) associated with a document, and the document does not fit within the model described by that schema, it is well-formed but not valid.

  4. If a schema is (or can be) associated with a document, and the document does not violate any of the constraints of that schema, it is well-formed and valid.

There are basically two kinds of validity which most people expect schemas to be able to test: the validity of content models and the validity of specific units of data.

Content Model Validity

Content model validity tests whether the order and nesting of tags is correct. Part 1 of the XML Schema WD defines how a schema indicates the correct order and nesting of elements.

An address, for example, might be defined as having a required <name> tag, one or more <street> tags, a required <city>, a required <state>, a required <zip>, and an optional <country> tag.

In XML Schema syntax, the content model of an address could be described like this:

<elementType name="address">
  <sequence>
    <elementTypeRef name="name" minOccur="1" maxOccur="1"/>
    <elementTypeRef name="street" minOccur="1" maxOccur="2"/>
    <elementTypeRef name="city" minOccur="1" maxOccur="1"/>
    <elementTypeRef name="state" minOccur="1" maxOccur="1"/>
    <elementTypeRef name="zip" minOccur="1" maxOccur="1"/>
    <elementTypeRef name="country" minOccur="0" maxOccur="1"/>
  </sequence>
</elementType>

(For a description of the syntax of XML Schema, see the syntax section.)

If you encounter a address that doesn't meet these criteria, it isn't valid (according to the address schema).

Datatype Validity

Datatype validity is the ability to test whether specific units of information are of the correct type and fall within the specified legal values.

For example, if I am writing a schema for catalog order forms, I should be able to express the constraint that the quantity ordered is greater than zero. An order form isn't valid if the quantity of an item ordered is "-5" or "blue".

The ability to express datatype validity in a schema is one of the really new features of XML Schema. Although database schema have always had this ability, XML DTDs do not. DTDs have extremly limited datatyping.

XML.com Copyright © 1998-2006 O'Reilly Media, Inc.