To understand XML, you need to step back and remember what HTML is. HTML is a markup language that uses a predefined set of tags to describe a document's structure in terms of paragraphs, headings, and so on. Like HTML, XML describes the structure of the document, but unlike HTML, XML is flexible enough (or extensible enough) to define the same tag name (such as <title>) in several different ways depending on which Document Type Definition (DTD) is called. In addition, XML takes the concept of tagging one step further by enabling developers to create custom tags and attributes. Both markup languages use style sheets to define the format of each tag with color, fonts, and emphasis. Document Type Definition (DTD)
The following examples show how a single entry from an address book might be marked up in both HTML and XML, respectively:
Why is this difference important? It's important because, in essence, your document becomes a giant database of information. Suppose that I am the owner of a chain of multiplex theaters and I want to put information on the Web about the movies I'm showing. In traditional Web publishing (if something as young as the World Wide Web can be said to even have a traditional method), I could do one of the following two things:
With the advent of XML, I have a third option. I can create a single Web page that contains all the information for all my theaters, and then use style sheets and templates to present the right information to the right people. Tip
Analyze the DataThe first thing I have to do is analyze my data. What information do I need to share? I probably would want to share the name of the movie, a brief description, the names of the stars in the movie, links to promotional information for the film, the name of the theater in which it's playing, the address of the theater, my phone number, the time the movie is showing, the price of the ticket, whether discounts are available, and a lot more. After you know the type of data you need to collect, you can create your XML input document. You can see an example of two of these input documents in Figure 17.1. Each data type is represented by a pair of tags (such as <movies> and </movies>). Related data types are nested within a parent tag. For example, the <title> and <star-male> tags are related to the <movies> tag. Unlike with HTML, I made up my own XML tags based on the information I wanted to present. Figure 17.1. Without a style sheet, an XML-enabled browser can only render text.Caution
Create a Style Sheet TemplateAfter you complete the input document, you need to create a style sheet template that determines how you present your information. You learned about style sheets in Lesson 5, "Adding Your Own Style." XML style sheet templates are very similar, but also define the structure of the document (tables, lists, paragraphs, and so on). Tip
The real fun with XML documents comes from the fact that the content of the page is separated from its format. In the movie theater example, suppose that I own two movie theaters. Multiplex 1 is a downtown art theater. It only shows artsy films attended by serious film students and it likes to promote itself as a dark, almost somber, environment. Multiplex 2 is in a posh part of uptown and shows mostly revivals to an older, more conservative crowd. Now imagine that I'm planning to show the same movie, Citizen Kane, at both theaters. My input document, which holds the content that appears on the Web site for both theaters, includes the following tags for Citizen Kane: <title>Citizen Kane</title> <star-male>Orson Wells</star-male> <desc>Powerful newspaper owner Charles Foster Kane was many things to many people, both in life and, as seen in retrospective, in death.</desc> <links>http://us.imdb.com/Plot?0033467</links> Using XML style sheet templates, I can create two completely different pages for my theaters. For Multiplex 1, the artsy theater, I might choose to have a black background with the title in a dramatic gothic-looking font and the other elements (<star-male>, <desc>, and <links>) placed in a bulleted list below. For Multiplex 2, the revival theater, I might create a background image of a film canister for my page. Then, I might choose to place all the elements of the movie into a horizontal table for a more conservative feel. I can do that because style sheet properties reference the element they are defining, not the content of that element. Rather than placing the content (Citizen Kane) on the style sheet template, I would place the following tag, which tells your computer to insert the information in the <title> tag. <xsl:value-of select="title"/> XML promises to be a platform-independent, software-independent language. Web developers and other programmers will be able to use the same data input documents to present information on the Web, in business automation tools (such as spreadsheets and word processors), and even on paper. That can save us all a lot of time and money. |