As the popularity of XML grows, new technologies that complement XML’s capabilities also continue to grow. The following section takes a quick tour of the important XML technologies that are essential to the understanding and development of XML-based ASP.NET Web applications. |
DTD |
One of the greatest strengths of XML is that it allows you to create your own tag names. But for any given application, it is probably not meaningful for any kind of tags to occur in a completely arbitrary order. If the XML document is to have meaning, and certainly if you’re writing a style sheet or application to process it, there must be some constraint on the sequence and nesting of tags. DTDs are one way using which constraints can be expressed. |
DTDs, often referred to as doctypes, consist of a series of declarations for elements and associated attributes that may appear in the documents they validate. If this target document contains other ele- ments or attributes, or uses included elements and attributes in the wrong way, validation will fail. In effect, the DTD defines a grammar for the documents it validates. |
The following shows an example of what a DTD looks like: |
<?xml version=”1.0” ?> <!-- DTD is not parsed as XML, but read by parser for validation --> <!DOCTYPE book [ <!ELEMENT book (title, chapter+)> <!ATTLIST book author CDATA #REQUIRED> <!ELEMENT title (#PCDATA)> <!ELEMENT chapter (#PCDATA)> <!ATTLIST chapter id #REQUIRED> ]> |
From the preceding DTD, you can already recognize enough vocabulary to understand this DTD as a definition of a book document that has elements book, title, and chapter and attributes author and id. A DTD can exist inline (inside the XML document), or it can be externally referenced using a URL. |
ADTD also includes information about data types, whether values are required, default values, number of allowed occurrences, and nearly every other structural aspect you could imagine. At this stage, just be aware that your XML-based applications may require an interface with these types of information if your partners have translated documents from SGML to XML or are leveraging part of their SGML infrastructure. |
As mentioned before, DTDs may either be stored internally as part of the XML document or externally in a separate file, accessible via a URL. A DTD is associated with an XML document by means of a |
<!DOCTYPE> declaration within the document. This declaration specifies a name for the doctype (which should be the same as the name of the root element in the XML document) along with either a URL reference to a remote DTD file, or the DTD itself. |
It is possible to reference both external and internal DTDs, in which case the internal DTD is processed first, and duplicate definitions in the external file may cause errors. To specify an external DTD, use either the SYSTEM or PUBLIC keyword as follows: |
<!DOCTYPE docTypeName SYSTEM “http://www.wrox.com/Books.dtd”> |
12 |
04_596772 ch01.qxd 12/13/05 11:17 PM Page 13 |
Introduction to XML |
Using SYSTEM as shown allows the parser to load the DTD from the specified location. If you use PUBLIC, the named DTD should be one that is familiar to the parser being used, which may have a store of commonly used DTDs. In most cases, you will want to use your own DTD and use SYSTEM. This method enables the parsing application to make its own decisions as to what DTD to use, which may result in a performance increase; however, specific implementation of this is down to individual parsers, which might limit the usefulness of this technique. |
As useful as DTDs are, they also have their shortcomings. The major concern most developers have with DTDs is the lack of strong type-checking. Also, DTDs are created using a strange and seemingly archaic syntax. They have only a limited capability in describing the document structure in terms of how many elements can nest within other elements. |
Because of the inherent disadvantages of DTDs, XML Schemas are the commonly used mechanism to validate XML documents. XML schemas are discussed in detail in a later section of this chapter. |
XDR |
XML Data Reduced (XDR) schema is Microsoft’s own version of the W3C’s early 1999 work-in-progress version of XSD. This schema is based on the W3C Recommendation of the XML-Data Note (http://www .w3.org/TR/1998/NOTE-XML-data), which defines the XML Data Reduced schema. |
The following document contains the same information that you could find in a DTD. The main difference is that it has the structure of a well-formed XML document. This example shows the same constraints as the DTD example, but in an XML schema format: |
<?xml version=”1.0” ? > <!-- XML-Data is a standalone valid document--> <Schema xmlns=”urn:schemas-microsoft-com:xml-data”> |
<AttributeType name=”author” required=”yes”/> <AttributeType name=”id” required=”yes”/> <ElementType name=”title” content=”textOnly”/> <ElementType name=”chapter” content=”textOnly”/> <ElementType name=”book” content=”eltOnly”> |
<attribute type=”author” /> <element type=”title” /> <element type=”chapter” /> |
</ElementType> |
</Schema> |
There are a few things that an XDR schema can do that a DTD cannot. You can directly add data types, range checking, and external references called namespaces. |