What is an XML Schema?
- Schemas define element types that can appear in a document.
- Schemas define attributes that can appear in a document.
- Schemas define which elements are child elements.
- Schemas define the order of child elements.
- Schemas define the number of child elements.
- Schemas define whether an element is empty or can include text.
- Schemas define data types for elements and attributes.
- Schemas define default and fixed values for elements and attributes.
- Schemas are mechanisms for one company, two companies or an entire industry to agree on a standard set of names (vocabulary) to identify and exchange data.
- Schemas provide applications with advance notice of what names (tags) and structures can be used in a particular document.
- Schemas centralize validation rules into text files that are easy to manipulate.
How Schemas are Different than DTDs:
- XML Schemas are written in XML. XML Schemas enable data type definition.
- XML Schemas support namespaces directly.
- XML Schemas enable element type and supertype hierarchy definition.
- XML Schemas support context-specific content models.
- XML Schema documents are not syntactically part of the documents they govern.
- Enables agreement on common vocabularies and structural rules across vertical and horizontal markets.
- Standardizes enterprise vocabularies to enable creation of standardized, interchangeable vocabulary modules.
- Enables syntactic validation of documents against the schema rules.
- Helps to ensure the correctness of data used by or created by a system.
How Schemas Work:
XML Schema documents define the element types (tag names) that are allowed within a particular document type. The schema document is then used by schema-aware XML processors to validate that documents governed by the schema conform to the rules defined in the schema document. Documents can point to a schema document, or a processing system can impose a schema on documents. For example, a processing system might have a rule that says “all documents must conform to schema X”. In that case, it is not necessary for the documents being processed to point to schema X—the system can simply validate the documents against schema X regardless of what schema, if any, they point to.
Schemas, like DTDs, define syntactic constraints. That is, they define what the names of elements and attributes are, what order they can appear, where they can appear, and what the spelling rules for attribute values and element content are (data types). But schemas cannot define more complex business rules. For example, you might have a business rule that says that if element “foo” contains the string “bar” then the document must contain elsewhere a “fred” element. This type of business rule cannot be expressed or enforced by a schema alone. Thus, while schemas provide a rich set of features for defining constraints, there will almost always be other rules that must be checked by a separate validation application . This means that you should not expect that just because you have a schema for your document type that the system is done.
It is also important to understand that schemas are designed specifically for defining constraints on XML documents. While the XML Schema mechanism has many of the features of a generic data or object modeling language, XML Schemas are not generic modeling tools. XML Schemas alone are not sufficient for doing the sort of system and object modeling required when designing and implementing complex information management systems. Rather, XML Schemas should reflect more abstract data models as one particular implementation view of the business objects the system reflects.