Valid XML

 <  Day Day Up  >  


A document that conforms to a DTD is said to be valid . Unlike many HTML document authors, SGML and XML document authors normally concern themselves with producing valid documents. With the rise of XML, Web developers can look forward to mastering a new skill: writing DTDs. The following example illustrates how XML might be used to create some structure for the combo meal example. A definition of the sample language to accomplish this task can be found within the document, although this definition can be kept outside the file as well. The burger2.xml file shown here includes both the DTD and an occurrence of a document that conforms to the language in the same document:

 <?xml version="1.0"?> <!DOCTYPE combomeal [ <!ENTITY cola "Pepsi"> <!ELEMENT combomeal (burger+, fries+, drink+)>     <!ELEMENT burger (name, bun)> <!ELEMENT name (#PCDATA)> <!ELEMENT bun (meat+, cheese+, meat+)> <!ATTLIST bun      bread (white  wheat) #REQUIRED >     <!ELEMENT meat EMPTY> <!ELEMENT cheese EMPTY>     <!ELEMENT fries EMPTY> <!ATTLIST fries      size (small  medium  large) #REQUIRED >     <!ELEMENT drink (#PCDATA)> <!ATTLIST drink      size (small  medium  large) #REQUIRED >         ]> <!-- the document instance --> <combomeal>    <burger>    <name>Tasty Burger</name>    <bun bread="white">       <meat />       <cheese />       <meat />    </bun>    </burger>    <fries size="large" />    <drink size="large">        &cola;    </drink> </combomeal> 

We could easily have just written the document itself and put the DTD in an external file referencing it using a statement such as

 <!DOCTYPE combomeal SYSTEM "combomeal.dtd"> 

at the top of the document and the various element, attribute and entity definitions in the external file combomeal.dtd. Regardless of how it is defined and included, the meaning of the defined language is relatively straightforward. A document is enclosed by the <combomeal> tag, which in turn contains one or more <burger>, <fries>, and <drink> tags. Each <burger> tag contains a <name> and <bun> , which in turn contain <meat /> and <cheese /> tags. Attributes are defined to indicate the bread type of the bun as well as the size of the fries and drink in the meal. We even define our own custom entity &cola; to make it easy to specify and change the type of cola, in this case Pepsi, used in the document.

One interesting aspect of using a DTD with an XML file is that the correctness of the document can be checked. For example, adding nondefined elements or messing up the nesting orders of elements should cause a validating XML parser to reject the document, as shown in Figure 18-3.

click to expand
Figure 18-3: Validation error message
Note  

At the time of this writing, most browser-based XML parsers don't necessarily validate the document, but just check to make sure the document is well- formed . The Internet Explorer browser snapshot was performed using an extension that validates XML documents.

Writing a DTD might seem like an awful lot of trouble, but without one, the value of XML is limited. If you can guarantee conformance to the specification, you can start to allow automated parsing and exchange of documents. Writing a DTD is going to be a new experience for most Web developers, and not everybody will want to write one. Fortunately, although not apparent from the DTD rules in this brief example, XML significantly reduces the complexity of full SGML. However, regardless of how easy or hard it is to write a language definition, readers might wonder how to present an XML document once it is written.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

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