- Why Should You Care About Valid XML?
- Data Type Validation--Another Reason for Validity
- How Do You Recognize Data Structure?
- You Can Parse for Validity
- Is Validity All That You Need?
- Summary
- Q&A
How Do You Recognize Data Structure?
The XML document listings you reviewed today, particularly Listings 3.1 and 3.2, shared many of the same elements, but characterized different structures. The order of the elements was not identical even though the element names were. As a result, one of the documents was intuitive and easy to follow (Listing 3.1), and the other (Listing 3.2) was not particularly representative of the desired markup.
Structure in the Recipe Markup Language
In XML, you will need to examine your documents very closely and understand the data structures, quite apart from any concern you might have for rendering the data in a browser. During Day 2 you reviewed the structure of a recipe and created two models to depict that structure. One was a block diagram (Figure 2.5) and the other offered a tree depiction of the recipe (Figure 2.6). You chose which items on the recipe card were important enough to warrant being dealt with as separate chunks of data. You eventually created the bean_dip3.xml file, shown in Listing 3.3.
Listing 3.3 bean_dip3.xml
1: <?xml version="1.0"?> 2: <!-- listing 3.3 - bean_dip3.xml --> 3: 4: <recipe title="Devan's Bean Dip"> 5: <ingredients> 6: <item quantity="1 can">refried beans</item> 7: <item quantity="1 can">burrito sauce</item> 8: <item quantity="8 oz.">cubed Jalapeno cheese</item> 9: <item quantity="1/2 cup">sour cream</item> 10: </ingredients> 11: <preparation>In medium sauce pan, heat bean 12: and burrito sauce until bubbly. Add cubed 13: cheese and stir until melted. Remove from 14: heat and stir in sour cream.</preparation> 15: <!-- For extra spicy bean dip, add your favorite hot 16: sauce to the mix --> 17: <serving>Serve as a dip with corn 18: tortilla chips</serving> 19: </recipe>
The process you undertook to map the structure of the recipe document was to create a model, a tree, something almost like a roadmap to the information contained on the recipe card. Then you defined element and attribute names that were self-describing and intuitive. You processed the information in a parser, thereby proving that it was machine readable and then had a look at it with your own eyes, to show that it was human intelligible. This is a fairly typical example of the approach you might take the next time you create an XML instance based on a document with a known structure. To add validity, you need only consider the rules that are associated with that structure and then author an associated schema to enforce those rules when the document is parsed. For the final recipe markup language you might have rules such as these:
There is one and only one recipe elementthe root elementthat contains all others.
The recipe element includes a required title attribute.
The recipe element must contain one each of ingredients, and preparation elements in that order, plus an optional serving element.
ingredients must contain at least one, and might contain any number of, item elements.
item elements have a required quantity attribute indicating the amount of each item needed.
You Might Already Be Using Structured Data
In business, you might already have document structures in place. In fact, some of these might even be rigidly enforced by either convention or policy. Like many companies, yours might have guidelines for correspondence, memoranda formats, invoice formats, and so on. Good document structure is typically more obvious than good data structure. When thinking about data structure, consider databases and the rigid structure enforced by integrated data dictionaries. This is a good analogy because the rules regarding database data structures, like those in XML, are more concerned about the characterization of the data than they are about how those data might be rendered in a form or a report. Dealing with output from a database requires the use of a report generator or another application, function, or query that is separate from the storage and validation of the data itself. The same is true with XMLXML on its own is all about the data and it requires the use of complementary, ancillary, technologies to produce output.
When you concern yourself with validity in XML, you are effectively concerning yourself with data structure, rather than presentation. Web developers have, for some time, had to consider the structure of e-content and the impacts of storage, retrieval, maintenance, and transfer of that material rather than merely how those data might be rendered in a browser window. Technical writers, documentation specialists and, to some degree, even attorneys creating contracts and legal briefs have had to deal with complex sets of predefined and stringently enforced document structures for many years. XML offers a means of codifying structures and sharing them with business partners and other users of information.
As noted, however, XML is all about the data. XML on its own has nothing to offer with regard to rendering of those data. So unlike a document structure that typically maps readily onto an output device designed to create paper reports, XML data structures are only meant to encapsulate the kernels of data themselves. When it comes to rendering, you'll use a variety of different, but complimentary approaches including, perhaps, cascading stylesheets, the Extensible Stylesheet Language (XSL), a part of XSL known as the Extensible Stylesheet Language Transformations (XSLT), and scripting to render data stored in XML structures. You will have an opportunity to master many of these techniques as you read the remainder of this book.
TIP
The fact that XML has the characteristic of keeping data structures separate from style and content is part of what makes XML interoperable by nature. The same XML instance can be used to ultimately generate paper reports, create CD-ROM storage media, and present data to browsers, Web-enabled cellular phones, personal digital assistants, and a variety of other user agents.
What then is the difference between document structure and data structure? Document structure provides a reader with an organized means of quickly following the path of information delivery desired by the author. When you write a memorandum to a colleague, you typically indicate to whom it is addressed, along with your own name as the author, the date, and perhaps a subject line or priority, followed by the body of the message. The recipient perusing the document can use the document structure to quickly determine all of these components and then follow along without getting lost or distracted by details that can be scanned quickly. Data structures reflect the content and provide computer applications with a roadmap, not unlike a keyword index, to the data stored in various containers and sub-containers within the context of the whole document. In data structure, there is no qualification of the importance of one document component over another. All are equally relevant and it is up to the application or XML processor to determine how to use the data obtained from each container.
You Can Use XML to Add Structure to Unstructured Information
XML offers you an unlimited mechanism to use structured data for information transfer. In fact, with XML it is easy to mix and match structured and less structured data, including text, numeric data, and strings to produce highly customized output streams to solve particular needs. You could accomplish the same end by using a variety of programming techniques, but XML was ideally designed with this kind of need in mind.
Table 3.3 is an extract from a database that contains courses offered by an XML training organization. The format of the database table has records for each course and fields that include Course_Number, Course_Name, and Instructor.
Table 3.3 Course Listings Database Table
Course Number |
Course Name |
Instructor |
---|---|---|
XML111 |
Introductory XML |
Bob Gonzales |
XML333 |
Advanced XML |
Devan Shepherd |
XMT222 |
XMetal Core Configuration |
Gene Yong |
If the data in this table were stored in a comma delimited text file, the result might look something like the document shown in Listing 3.4.
Listing 3.4 Comma Delimited Text Version of Database Records
Course_Number,Course_Name,_Instructor XML111,Introductory XML,Bob Gonzales XML333,Advanced XML,Devan Shepherd XMT222,XMetal Core Configuration,Gene Yong
XML provides a means to supplement these data with additional information that might be of value to humans or to computers. For instance, Listing 3.5 shows the same data included with additional information that could be used to produce a press release about upcoming course offerings.
Listing 3.5 Highly Structured Data Obtained from a Database Combined with Additional Information in XML
1: <?xml version="1.0"?> 2: <!-- listing 3.5 - courses.xml --> 3: 4: <announcement> 5: <dist>For Immediate Release</dist> 6: <to>All Potential Students</to> 7: <from>Devan Shepherd</from> 8: <subject>Public Course Offerings in August</subject> 9: <notice>ACME Training is pleased to announce the following 10: public courses, which are offered on a monthly basis.</notice> 11: <contact>For more information, or to register for any of these courses, 12: please visit 13: <Web site>http://ACME-Train.com/university</Web site></contact> 14: <courses> 15: <course id="XML111" instructor="Bob Gonzales"> 16: Introduction to XML</course> 17: <course id="XML333" instructor="Devan Shepherd"> 18: Advanced XML</course> 19: <course id="XMT222" instructor="Gene Yong"> 20: XMetal Core Configuration</course> 21: </courses> 22: </announcement>
The XML output is now infinitely more useful than the comma delimited text version of the data, not only because of the added information, but also because of the use of attributes within the course elements. This XML source can be used to create an e-mail message, a newsletter, or other printed document. The same data can also be transformed, under program control, into another markup language for use by a commercial advertising service. Using XML technologies, you could also create output from the same source file for Web-enabled wireless devices, such as cellular phones, Personal Digital Assistants, pagers, and so on.
You will learn on Day 14 to use cascading stylesheets (CSS). On Days 15 and 16, you will learn the Extensible Stylesheet Language (XSL) and the subset of XSL, Extensible Stylesheet Language Transformations (XSLT), to style output like this for use in a browser or other user agents, devices and processors.
NOTE
Valid XML guarantees the integrity of data structure, as though a contract has been established with the XML instance. Automation of content delivery is simplified because of this guarantee and can be affected using XSLT.