XML Is About Data

If HTML is about displaying information, XML is about describing information. XML is a standard language that structures and describes data and is also understood by different applications. The power of XML is its ability to separate the user interface from the data. Let's take a look at a simple XML document (chap 21\email.xml on the companion CD) to see how this works.

    <?xml version="1.0"?>    <EMAIL>      <TO>Bill_Pardi@hotmail.com</TO>      <FROM>Eric_Schurman@hotmail.com</FROM>      <CC>DHTML_in_Action@hotmail.com</CC>      <SUBJECT>Basic email</SUBJECT>      <BODY>This is an XML-based email.</BODY>    </EMAIL> 

What is important to notice in this example is that nothing in the document indicates how the data should be displayed. In other words, no formatting information (such as bold or italic fonts, text indent, and font size) is included. Because the document code describes what the data is, a reader can easily understand the content and structure of the document.

XML documents are also known as self-describing. That is, each document contains the set of rules to which its data must conform. Because any set of rules can be reused, it is easy for authors to create new versions of the same class of document.

NOTE
The class concept was borrowed from object-oriented programming, in which a class is used to describe a group of objects with a common set of characteristics. Classing documents provides a powerful way to group documents based on the kind of content they contain.

Valid and Well-Formed XML

Two valuable features of XML are its ability to provide structure to documents and its ability to make data self-describing. For these features to work, however, structural and grammatical rules must be in place. The next two sections give an overview of two terms, valid and well-formed, which describe documents that obey the structural and grammatical rules that govern XML.

Valid Documents

The XML processor helps enforce structure and content rules in an XML document. These rules include those required by the standard XML specification, as well as any special rules defined for that document. This second set of rules comes in the form of a Document Type Definition (DTD). A DTD is separate from the actual data in an XML document and can exist either as part of the XML document or in a separate document. A valid XML document strictly obeys both types of these rules. This book does not detail how DTDs are created and used because it focuses on simple examples that do not use DTDs. You can find out more about DTD creation from any good XML reference book or Web site, including XML in Action by William J. Pardi (Microsoft Press, 1999) and www.w3.org/xml. As you will see, however, DTDs are not always required for XML documents.

If the processor finds any errors in the XML document, it reports them to the XML application. The process of reading the DTD, checking the document against it, and reporting violations is known as validating the document. Because all of this processing and checking takes time, and because validation is not always necessary, XML supports the notion of the well-formed document. A document that meets the criteria for well-formedness is not checked for validity and is therefore processed faster.

Well-Formed Documents

The rules for well-formed documents are not nearly as strict as those governing valid documents. Creation of a well-formed document is fairly easy and intuitive. The following list outlines the core rules of well-formedness. Examples follow later in this chapter.

  • There is exactly one root element (also called the Document element).
  • No part of the root element can appear in the content of any other element in the document.
  • For all other elements in the document, both the start and end tags must be nested at the same level of the document. That is, if the start tag is placed in the content of one element, the end tag is placed in the content of that same element.

If the XML document doesn't conform to these specifications, the processor will report the errors. In Internet Explorer, the errors can be accessed with script through the Object Model. Error handling is covered in greater detail later in the chapter.

Let's look at a sample XML document:

 <?xml version="1.0"?> <PLANT>   <COMMON>Columbine</COMMON>   <BOTANICAL>Aquilegia canadensis</BOTANICAL> </PLANT> 

This document contains the PLANT element as the single Document element, and the Common and Botanical elements are nested inside the Document element. To illustrate this concept, the following example is not well-formed XML because it contains two elements at the root:

 <?xml version="1.0"?> <COMMON>Columbine</COMMON> <BOTANICAL>Aquilegia canadensis</BOTANICAL> 

Element nesting sets up parent/child relationships. Every child element (an element that is not the root element) resides within its parent element, similar to the Dynamic HTML Object Model discussed in Chapter 5. This can be represented as follows:

 <ROOT>  <PARENT1>    <CHILD1></CHILD1>    <CHILD2></CHILD2>  </PARENT1> </ROOT> 

A child element is never located within any of its parent's other children. For example, the following code is not well-formed XML:

 <ROOT>  <PARENT1>    <CHILD1>     <CHILD2></CHILD1></CHILD2>  </ROOT> </PARENT1> 

Also, no part of the Document element can reside within any of its children. This code, for example, is not considered well-formed XML:

 <ROOT>  <PARENT1>    <CHILD1></CHILD1>     <CHILD2></CHILD2>  </ROOT> </PARENT1> 

As you can see, XML documents are highly structured and must follow strict but simple rules to be well-formed and valid. This structure imposes a specific hierarchical order that results in a "family tree" of parent/child elements.



Dynamic HTML in Action
Dynamic HTML in Action
ISBN: 0735605637
EAN: 2147483647
Year: 1999
Pages: 128

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