XML Schemas Are a Type of Schema

XML Schemas offer an alternative to describing an XML grammar using DTDs. They do not “replace” DTDs, nor do XML Schemas necessarily make DTDs obsolete. DTDs are simple, and they are established technology. There are times when in the interest of expedience and compatibility, it would make sense to use a DTD.

Both tools are useful for describing XML documents. If backward compatibility is an issue, DTDs might be a wiser choice. But if you need some of the features XML Schemas offer, such as datatypes, then the choice will be made for you. It's simply a question of using the right tool for the job.

From the XML Schema Requirements Note published by the W3C, the goals of the XML Schema Recommendation state that XML Schemas should be

  • More expressive than XML DTDs

  • Expressed in XML

  • Self-describing

  • Usable by a wide variety of applications that employ XML

  • Straightforwardly usable on the Internet

  • Optimized for interoperability

  • Simple enough to implement with modest design and runtime resources

  • Coordinated with relevant W3C specs (XML Information Set, Links, Namespaces, Pointers, Style, and Syntax, as well as DOM, HTML, and RDF Schema)

Now, let's take a look at what schemas have to offer.

Schemas Go Beyond DTDs

The Document Type Definition is a great way to express XML grammar. However, there are some limitations to the functionality that can be achieved with a DTD. DTDs don't allow inheritance or scoping. And DTDs don't allow for datatyping. These are features that can be very useful in XML design, and thus Schemas are designed to include these types of functionality.

Schemas Are XML

One of the biggest advantages of working with XML Schemas is that XML Schemas are actually XML documents themselves. Unlike DTDs, XML Schemas follow the structure of a well-formed XML document.

Let's look at a very simple XML Schema:

<?xml version="1.0" encoding="utf-8" ?> 
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="Name" type="xs:string"/>
</xs:schema>

Now, this is a very simple XML Schema that defines an element called name that is a string. That's all it does, which isn't very useful in itself.

However, take a closer look at the Schema. Notice that it starts with an XML declaration:

<?xml version="1.0" encoding="utf-8" ?> 

This is the same declaration that starts any XML document. It lets an application know that the document is an XML document and that it is based on the 1.0 XML Recommendation, and uses the UTF-8 character encoding set. This declaration should already be familiar to you from well-formed XML documents. It is used with XML Schemas because XML Schemas are well-formed XML documents.

This is a very important feature of XML Schemas because it allows any application that is already XML aware to process XML Schemas. Because they are XML, the same parser can parse schemas as well as XML documents. Unlike DTDs, which have their own complicated syntax, XML Schemas are based on well-formed XML and make extensive use of Namespaces to define elements, attributes, datatypes, and so on. This makes it much easier for software developers to incorporate schema support into their applications.

In fact, if you look at the next line in the sample Schema, it is a Namespace declaration:

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

This is also important, as it provides a mechanism for scoping and versioning, which we will discuss later.

Finally, this defines the “xs” Namespace and points back to the XML in which we get to the meat of the Schema, the actual element declaration:

<xs:element name="name" type="xs:string"/> 

The declaration makes use of our Schema namespace, and it uses attributes to provide information about the element we are defining. We will discuss namespaces in greater detail in Chapter 6. Here we define an element called name and we also go on to specify that it is a string. That type attribute is a critical component of XML Schemas, as it is what allows us to do datatyping.

Schemas Are Far Reaching

Because XML Schemas are written using XML, that allows Schemas to be adopted quickly and easily by applications already using XML. This helps Schemas meet the requirements of the Working Group that Schemas be “usable by a wide variety of applications that employ XML; and straightforwardly usable on the Internet.” Because XML is text based, it is easily transferable via common Internet file transfer methods, such as FTP and HTTP. This means that accessing XML files, and therefore XML Schemas, is as simple as mailing an attachment or visiting a Web page.

In fact, Schemas are “optimized for interoperability” in this way because they are recursive, self-describing documents. By employing XML as the language of Schemas (unlike DTDs, which have their own complex syntax), Schemas are already optimized to work with other XML-based applications.

Schemas Allow Datatyping

Of course, the fact that XML Schemas are actually XML is significant. But the main reason people are looking to XML Schemas is for datatyping.

Datatypes allow you to specify that a piece of information has to be in a specific data format. For example, a string type is a concatenation of characters. An integer would need to be a whole digit, such as 1, 2, 3, and so on.

Datatypes are a very powerful mechanism for placing constraints on your XML documents. We'll discuss datatypes in much greater detail later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183