Schemas and XML Data Modeling
The process of creating a schema for an XML document is known as data modeling because it involves resolving a class of data into elements and attributes that can be used to describe the data in an XML document. Once a data model (schema) is in place for a particular class of data, you can create structured XML documents that adhere to the model. The real importance of schemas is that they allow XML documents to be validated for accuracy. This simply means that a schema allows an XML developer (or an application) to process a document and see if it adheres to the set of constraints laid out in the schema. If not, you know the document could prove to be problematic. A valid XML document is kind of like a stamp of approval that declares the document suitable for use in an XML application. You learn how to validate your own XML documents in Hour 8, "Validating XML Documents."
To help clarify the role schemas play in XML, let’s consider a practical real-world analogy. Pretend you just met a friend whom you haven’t seen in years and she gives you her email address so that you can get in touch with her later. However, she lists her email address as lucy*stalefishlabs.com. You know that all email addresses consist of a name followed by an "at" symbol (@), followed by a domain name, which means that something is wrong with her email address. The name@domainname format of email addresses is actually a simple schema—you used this schema to "validate" your friend’s email address and determine that it is in error. The obvious fix is to replace the asterisk (*) in her email address with an "at" symbol (@).
You now understand in the simplest of terms how schemas are used to determine the validity of XML documents, but you don’t entirely know why. The main reason schemas are used in XML is to allow machine validation of document structure. In the invalid email example, you were easily able to see a problem because you knew that email addresses can’t have asterisks in them. But how would an email application be able to make this determination? The developer of the application would have to write specific code to make sure that email addresses are structured to follow a given syntax, such as the name and domain name being separated by an "at" symbol. Whereas an email application developer writes code to check the validity of an email address, an XML document creator uses a schema. This schema can then be used by XML applications to ensure that documents are valid; schemas provide a mechanism to facilitate the process of validating XML documents.
When it comes to creating schemas, there are two primary approaches you can take:
- Document Type Definitions (DTDs)
- XML Schemas (XSDs)
These two schema approaches represent different technologies that make it possible to describe the data model of an XML-based markup language. The next two sections explain each approach in more detail.
Document Type Definitions (DTDs)
Warning, I’m about to roll out a new acronym! The new acronym I want to introduce you to now is DTD, which stands for Document Type Definition. DTDs represent the original approach of creating a schema for XML documents. I say "original approach" because DTDs did not originate with XML; DTDs originated with XML’s predecessor, SGML (Standard General Markup Language). DTDs made their way into XML because it eased the transition from SGML to XML—many SGML tools existed that could be used for XML. Things have changed since the early days of XML, however, and now there is a more powerful approach to establishing schemas than DTDs. Even so, DTDs are still in use so it’s important for you to understand how they work.
The main drawback to DTDs is that they are based upon a somewhat cryptic language. XML provides a highly structured approach to formatting data, so why should you have to learn an entirely new language to describe XML schemas? I don’t have a good answer to this question except to say that DTDs are a carryover from XML’s beginnings and they still play a role in some XML applications, so you should learn how to use them. The good news is that DTDs are actually quite simple for describing most XML-based markup languages. This is due to the fact that the DTD language is extremely compact, which is why it has a cryptic appearance. Rather than continue to describe DTDs in words, let’s just look at an example in Listing 3.1.
Listing 3.1 A Simple DTD for the Tall Tales XML Document
1: <!ELEMENT talltales (tt)+> 2: 3: <!ELEMENT tt (question, a, b, c)> 4: <!ATTLIST tt 5: answer (a | b | c) #REQUIRED> 6: 7: <!ELEMENT question (#PCDATA)> 8: 9: <!ELEMENT a (#PCDATA)> 10: 11: <!ELEMENT b (#PCDATA)> 12: 13: <!ELEMENT c (#PCDATA)>
I warned you it was kind of cryptic. However, if you take a moment to read through the DTD code you can actually start to make some sense of it. You might even recognize that this DTD is for the Tall Tales trivia document that you saw in the previous hour. By studying the code, you can see that the word ELEMENT precedes each element that can be used in a TTML (Tall Tales Markup Language) document. Also the attributes for the tt element are listed after the word ATTLIST (line 4); in this case there is only one attribute, answer (line 5). Also notice that the three possible values of the answer attribute (a, b, and c) are listed out beside the attribute (line 5). Although there are a few strange looking pieces of information in this DTD, such as the <! at the beginning of each line and (#PCDATA) following each element, it’s pretty apparent that DTDs aren’t overly complex.
You learn a great deal more about DTDs later in this hour, so I won’t go into more detail just yet. Instead, we’ll move on and learn about the other approach to data modeling that uses a syntax that should be very familiar to you.
XML Schema (XSDs)
XML Schema replaces DTDs with a more powerful and intuitive approach to creating schemas for XML-based markup languages. Schemas created using XML Schema are coded in the XSD (XML Schema Definition) language, and are therefore referred to as XSDs. XML Schema and the XSD language were created by the W3C (World Wide Web Consortium), and represent a considerably more powerful and flexible approach to schemas than DTDs. The idea behind XML Schema is to use XML as the basis for creating schemas. So, instead of using the special DTD language to create a schema, you can use familiar XML elements and attributes that are defined in the XSD language.
An XSD is very similar in purpose to a DTD in that it is used to establish the schema of a class of XML documents. Similar to DTDs, XSDs describe elements and their content models so that documents can be validated. However, XSDs go several steps beyond DTDs by allowing you to associate data types with elements. In a DTD, element content is pretty much limited to text. An XSD is more flexible in that it can set the data type of elements to specific types, such as integer numbers and dates.
Of course, the most compelling aspect of XSDs is the fact that they are based upon an XML vocabulary (XSD). This means that you create an XSD as an XML document. So, the familiar tag/attribute approach to encoding XML documents is all you need to know to code an XSD document. You still have to learn the specific elements and attributes that comprise the XSD language, but it isn’t too terribly difficult to learn. To give you an example, the code in Listing 3.2 is for an XSD that describes the familiar Tall Tales document.
Listing 3.2 An XSD Document That Serves as a Schema for the Tall Tales XML Document
1: <?xml version="1.0"?> 2: 3: <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> 4: 5: <xsd:element name="talltales" minOccurs="1" maxOccurs="1"> 6: <xsd:complexType> 7: <xsd:element name="tt"> 8: <xsd:complexType> 9: <xsd:sequence> 10: <xsd:element name="question" type="xsd:string" maxOccurs="1" /> 11: <xsd:element name="a" type="xsd:string" maxOccurs="1" /> 12: <xsd:element name="b" type="xsd:string" maxOccurs="1" /> 13: <xsd:element name="c" type="xsd:string" maxOccurs="1" /> 14: </xsd:sequence> 15: <xsd:attribute name="answer" type="answerType" use="required" /> 16: <xsd:simpleType name="answerType"> 17: <xsd:restriction base="xsd:NMTOKEN"> 18: <xsd:enumeration value="a" /> 19: <xsd:enumeration value="b" /> 20: <xsd:enumeration value="c" /> 21: </xsd:restriction> 22: </xsd:simpleType> 23: </xsd:complexType> 24: </xsd:element> 25: </xsd:complexType> 26: </xsd:element> 27: 28: </xsd:schema>
As you can see, XSDs aren’t nearly as compact as DTDs, and can be more difficult to understand initially. The reason for this is because XSDs are considerably more powerful and flexible than DTDs, and with advanced features comes complexity. You learn all about creating XSDs in Hour 7, "Using XML Schema XSDs," after which this code will make complete sense to you. For now, the main thing to understand is that XML Schema allows you to use XML code to model data in a more detailed manner than DTDs. For example, in an XSD you can specify exactly the number of times an element is allowed to appear when nested.