XML vs. HTML

   



DTDs

An XML document not only contains information in a predictable format, it can also describe the organization of the information it contains. The beginning of an XML document may contain a document type definition (DTD). This section of the XML document defines the structure of the document's contents. It identifies the tags that are allowed in this document and the relationships between the various tags in the document. An XML parser can use this information to make sure that the document is not only well formed (that is, it conforms to the generic XML syntax rules), but also valid (that is, conforms to the layout specified in its DTD).

For example, our XML shopping cart document could contain this partial DTD:

<!ELEMENT Order (LineItem)+> <!ATTLIST Order orderNumber CDATA #REQUIRED> <!ELEMENT LineItem (Title, Author+, BookType, Price)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT BookType (#PCDATA)> <!ELEMENT Price (#PCDATA)> <!ENTITY HARDCOVER "Hardcover"> <!ENTITY PAPERBACK "Paperback"> 

The first statement in this DTD describes an XML element whose name is Order. According to this DTD, an Order consists of one or more LineItem elements (represented by the (LineItem)+ expression in our example). The Order must be further described using the attribute named orderNumber. An orderNumber attribute can contain any valid character sequence (CDATA). It is a required attribute, as specified by the #REQUIRED flag.

A LineItem, in turn, consists of a Title, one or more Authors (as indicated by the command Author+), a BookType, and a Price. These sub-elements are all user-defined data types, which can contain any character sequence (#PCDATA).

The last section of our DTD declares two variables named HARDCOVER, and PAPERBACK. These variables are assigned the String values 'Hardcover' and 'Paperback,' respectively.

A DTD not only provides the structural information required to properly parse the document, it also allows the parser to examine the document for completeness and document integrity. If a DTD specifies that certain elements are required, then these elements must be present in the document. Conversely, only properly identified elements are allowed in an XML document.

An input parser application can quickly scan XML documents to validate that they match their required structure, independent of any data or content validation. An XML document can be independently checked to make sure it is both well formed (it conforms to proper generic XML syntax) and valid (it conforms to its DTD).

An XML document can either contain its DTD (that is, as the initial part of the document) or simply refer to it as a external file. Either a URL or a local file name can be referenced as the DTD repository.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net