Integrating the DTD into the XML Document

 <  Day Day Up  >  

Now that we have created the DTD, it is time to create an actual XML document. Remember that the XML document must conform to the supplier DTD we have just written.

In Table 12.2, we have identified some of the actual information that will be contained in the XML document. Again, note that the data is only contained in the end elements, not the aggregate elements, such as address and name .

Table 12.2. Adding the Values to the Table

Object

Category

Field

Value

supplier

     

name

   
 

<companyname>

The Beta Company

address

   
 

<street>

12000 Ontario St

 

<city>

Cleveland

 

<state>

OH

 

<zip>

24388

product

   
 

< type>

Vacuum Cleaner

 

< price>

50.00

 

< count>

20

To enter this information into an XML document, we can use a text editor, just as we used for the DTD. However, as we will see later, there are tools that have been created specifically for this purpose. Figure 12.4 shows the XML document written using Notepad. This document is called beta.xml . Note that the second line ties this document to the supplier DTD that we defined earlier.

 
 <!DOCTYPE supplier SYSTEM "supplier.dtd"> 
Figure 12.4. The Beta Company XML document with the DTD.

graphics/12fig04.jpg

Looking at Figure 12.4, we can see that the tag structure mimics the specification. It is important to realize that the tags are nested, and that only the end tags contain any data. For example, the <street> tag does contain information, whereas the address tag does not:

 
 <address> <street>12000 Ontario St</street> 

There is a better way to inspect the XML document. As stated previously, there are many tools that have been written to assist in the development of XML documents. One of these tools is called XML Notepad, and it has a similar look and feel to Notepad.

XML Notepad

Microsoft does not provide XML Notepad at this time. You can still find XML Notepad by doing a simple Internet search for "XML Notepad." You can download Microsoft's XML Validator at:

http://msdn.microsoft.com/downloads/samples/internet/xml/xml_validator


XML Notepad can help us understand the structure of an XML document. After you install XML Notepad, you can open the beta.xml file. Figure 12.5 shows what happens when you open the beta.xml file with XML Notepad. When the document opens, expand all of the plus signs to look at all the elements. XML Notepad lists each level of the document, starting with the supplier tag. Note that as we have said before, only the end elements contain any information.

Figure 12.5. Opening the beta.xml file with XML Notepad.

graphics/12fig05.jpg

The obvious advantage to developing the DTD is that it can be used for more than one document; in this case, for more than one supplier. Let's say we have a company that makes skates called Gamma Company, which wants to supply Alpha Company. What Gamma Company needs to do is create an XML document that conforms to the supplier DTD. Opening up this document with XML Notepad presents the picture seen in Figure 12.6.

Figure 12.6. Opening the gamma.xml file with XML Notepad.

graphics/12fig06.jpg

Note that beta.xml and gamma.xml conform to the supplier DTD. The question is, what happens when the XML document does not conform to the DTD? It is at this point where we see the power of the DTD. Let's purposely create an error in the gamma.xml file by taking out all the information pertaining to the name.

 
 <name> <companyname>The Gamma Company</companyname> </name> 

Basically, we are creating an invalid document ”invalid per the supplier DTD. The invalid document is found in Figure 12.7. Be aware that Notepad will not indicate that the document is invalid because Notepad does not check for validity. You need to use an XML validator to check for validity.

Figure 12.7. An invalid document (no name information).

graphics/12fig07.jpg

We now have an invalid document based on the supplier DTD. How do we verify that it is invalid? We can open the invalid gamma.xml document with XML Notepad. Notice the result, as indicated in Figure 12.8. Here XML Notepad provides a dialog box that specifies that an invalid document was detected .

Figure 12.8. The invalid document error in XML Notepad.

graphics/12fig08.jpg

Because the supplier DTD was expecting a document to conform to its definition, an error was generated. In fact, the error message is quite specific as to what the problem is. The DTD was expecting the name information. Thus, to create a proper XML document for this system, all the appropriate information must be supplied and supplied in the proper format. The <address> tag must be provided for the document to be valid.

One of the primary points to recognize here is that this error checking would not have happened in HTML. In fact, you can open up the XML file with a browser, as seen in Figure 12.9.

Figure 12.9. The beta.xml document opened in Internet Explorer.

graphics/12fig09.jpg

Now let's see what happens when we open up the invalid gamma.xml document with a browser. Figure 12.10 shows the gamma.xml file when it's opened in Internet Explorer.

Figure 12.10. The invalid gamma.xml document opened in Internet Explorer.

graphics/12fig10.jpg

Note that even though the document is invalid, the browser opens it and even displays it. This is because the browser is not checking to make sure the document conforms to the DTD, whereas XML Notepad does perform this check. In theory, this is one of the major advantages that XML provides when working with data. Although HTML is used to display the data, XML is used to format the data. This is a very important distinction.

You might ask, what benefit does XML Notepad provide in the overall supplier example, and what is it used for? To answer the first part of the question, XML Notepad, or some editor like it, allows us to verify that the document is valid early on in the process. To answer the second part of the question, XML Notepad or a similar editor can be used to actually construct the document.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

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