What Is XML?


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)

A file defining the set of tags that can be used within a particular file. XHTML uses three DTDs: strict, transitional, and frameset.


The following examples show how a single entry from an address book might be marked up in both HTML and XML, respectively:

  • HTML:

    <p>The White House<br /> 1600 Pennsylvania Avenue NW<br /> Washington, DC  20500</p>

  • XML:

    <contact>   <name>The White House</name>   <address>1600 Pennsylvania Avenue NW</address>    <city>Washington</city>   <state>DC</state>   <zip>20500</zip> </contact>

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:

  • Create a series of Web pages that would need to be updated frequently.

  • Create a database that held all the information and hire a Java programmer to write an application that would enable people to perform searches on my database to see what was showing in their neighborhood.

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

The W3C and industry experts are creating industry-specific versions of the XML standard. So, you can create your own tags in XML, and you can also take advantage of the fact that others in your industry are using the same standard.


Analyze the Data

The 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

Don't rush out to convert all your HTML documents to XML just yet. Most browsers can't process XML documents yet. However, you can start preparing now by creating XHTML documents. These documents enable you to use HTML and XHMTL now, and will be easy to convert to XML in the future.


Create a Style Sheet Template

After 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

You can learn more about XML style sheet templates from the W3C at www.w3.org/TR/xsl/. Another excellent resource for XML information is www.xml.com.


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.



Sams Teach Yourself HTML in 10 Minutes
Sams Teach Yourself HTML in 10 Minutes (4th Edition)
ISBN: 067232878X
EAN: 2147483647
Year: 2004
Pages: 151
Authors: Deidre Hayes

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