I Remember 1998. XML was brand new, having just been approved as a recommendation by the W3C. Designers were curious about the language and, more specifically, about whether it would ever replace HTML. Because it turned out the answer was “no, XML serves different needs than HTML,” they happily abandoned XML to programmers.
Five years later, there’s a better understanding of XML and its capabilities. This leads to renewed interest in XML for Web publishing. Unfortunately, there’s still a lot of confusion on how to effectively apply XML for Web publishing.
Let me show you how to create a simple Web with XML — in minutes.
XML is designed to publish documents programmatically. A developer or a designer writes stylesheets (XML jargon for scripts) that control the layout and navigation of your site.
This is not unlike ASP or PHP, where scripts queries databases and format the result into a Web site. Indeed, XML, ASP, and PHP are functionally similar, although, as you will see, very different in practice.
One of the differences is that XML is a standard; the others are products. A standard is a blueprint for products so, whereas there is only one implementation of ASP and one of PHP, there are dozens of XML products to chose from.
More importantly, the different products implement different solutions, so they are suitable for different types of projects. For example, Cocoon is ideal for highly dynamic sites, Vignette excels with very large sites, and XM is for medium-sized sites.
No single product, no matter how good, can give you this much flexibility. You need a standard.
As an illustration of how easy publishing with XML can be, let’s use XM to prepare a simple Web site.
I suggest you install the software and follow along. XM is most often used as an Eclipse plug-in, so you will need to download Eclipse as well. At the time of writing, Eclipse 3.0 is a moving target, so you are better off with Eclipse 2.1. Refer to the instructions that come with the XM download.
Having installed the software, create a new project. Choose File|New Project. In the project window, choose ananas.org and XM Project (see Figure 1). This will create a new project with a sample document.
Figure 2 shows the new project in the navigator. Notice the three directories: src, rules, and publish.
Look under publish and you will find a one-page Web site. Wow! You’ve just published your first site with XML.
The src directory is where XM stores the documents. The project wizard has created a sample document. Open it.
The reliance on documents is another difference between XML and ASP/PHP. Instead of storing data in a relational model (SQL databases), it uses a hierarchical model (XML documents). Hierarchies are well suited for documentation, including books, articles, technical documentation, and the like.
You can edit this document or create more documents in the src directory. If you create new or different tags, you will need to adapt the sample stylesheet as well (see below).
Concerned about writing XML documents? Don’t be! Tools exist to convert from most popular document formats to XML. upCast is an example of Word to XML, and HTML Tidy performs similar services for HTML documents.
The next directory is rules and it contains the XSLT stylesheets. Again, the wizard has created a sample stylesheet. Think of stylesheets as scripts that query the XML documents and format the result into a Web site.
Technically, stylesheets are organized as lists of templates where each template describes how to format an XML tag. You use a powerful scripting language (XSLT) to generate precisely the layout you want. Your imagination is the only limit with stylesheets. In addition to the layout, you also can create tables of contents, indexes, and other navigational aids.
If you add new tags in the XML documents or if you don’t like the sample layout, you will need to edit the stylesheet.
That’s it! Publishing with XML is no more complex than this. Install a tool, prepare XML documents, adapt the corresponding XSLT stylesheets, and you have a Web site.
As you have seen, XML is functionally similar to ASP, PHP, and other scripting tools inasmuch as it lets you build the Web site programmatically. The most significant benefits of XML when compared to other scripting tools are that:
- It’s a standard. Competing implementations will allow you to choose the product most suited to your needs.
- It boasts an hierarchical data model that is better suited for documentation than SQL databases.
About the Author
Benoît Marchal is a Belgian writer and consultant. He is the author of XML by Example and other XML books. He works mostly on e-commerce, XML, and Java.