XML to the Rescue

I l @ ve RuBoard

Before we get into a detailed discussion of how to use XML, we should introduce some of its basic concepts and technologies. This should provide enough background for you to understand the advantages of incorporating XML technology into XP Web development projects.

Basic XML

XML is a markup language similar to HTML. An XML document is simply a text file containing a hierarchical collection of nodes , such as elements, attributes, processing instructions, and text. An important property of XML is its support of semantic rather than formatting markup. Unlike HTML, which uses a set of predefined tags, XML gives developers the freedom to mark up their content semantically and to make their own tags. For example, a product can be represented using tags such as <product> , <code> , <price> , < size > , and <description> .


One of the cornerstones of XML is its use of parsers to read XML files and identify the XML nodes within. A number of XML parsers are available in many different programming environments. All of them present an API to read or write to the nodes in an XML document. Without a parser, an XML document is nothing but a text string that is difficult to process or modify.


An important function of XML parsers is document validation ”that is, checking an XML document for conformance with a schema document or DTD. The base XML specification defines the concept of a validating parser . This is simply a generic XML parser that is capable of validating any XML document given the appropriate schema. The parser reads the XML file and the schema document and generates an error if any of the rules specified in the schema are violated.


The schema document defines the valid structure of the XML content. Schemas allow you to specify the element names allowed in a document, the attributes allowed in each element, and the way elements can be nested. They also allow you to specify certain data types for attributes and text nodes.

Validation is a simple concept but a very powerful one. For XP projects XML validation adds a new technique to the unit testing toolkit.

Schemas versus DTDs

Schemas and DTDs are two ways to specify the structure of an XML document. DTDs are an older standard and come out of XML's SGML roots. Schemas are more recent and much more sophisticated. Unlike DTDs, schema documents are themselves well- formed XML and can be read with an XML parser. It is preferable to work with schemas rather than DTDs for XML validation, however, everything in this chapter applies equally well to both.


Once you have marked up your content in XML, you need a way to specify how it is to be formatted and displayed on a Web page. This is where XSLT comes in.

XSLT, a sister technology to XML, is a transformation language designed to translate XML documents from one format to another. You can use it to output any kind of formatted text. Depending on how you write them, XSLT templates can output flat text files, CSV files, HTML, XHTML, or any kind of XML. For Web development the common approach is to use XSLT to transform semantically marked up XML page content into display-formatted HTML or XHTML Web pages.

XSLT processors are for many platforms that conform to W3C standards, including Java (Xalan) and Microsoft (MSXML), which means that XSLT style sheets are portable.

Learning XSLT

Some developers find XSLT difficult to work with at first. This is likely because it is a declarative, rule-based language rather than a procedural or object-oriented one. Learning XSLT requires you to leave behind a number of familiar programming concepts and to start thinking in terms of transformations, match patterns, namespaces, node sets, result tree fragments , and the like. It has often been said that years of programming procedurally can be an obstacle to learning object-oriented programming, and we think the same can be said for learning XSLT. Still, our experience is that, once mastered, XSLT is a powerful, intuitive, flexible, and elegant language to work in.

Client-Side XSLT

In a Web environment you have the choice of performing the XSLT transformation on the client or on the server. Web browsers such as Internet Explorer 6.0 and Netscape Navigator 6 ship with fully W3C-compliant XSLT processors. In an intranet environment or administrative backend to a public Web site, where you can require users to install level-6 browsers, you can develop entire Web applications that send nothing but XML to the browser and have all of the XSLT transformations done on the client side. For public Web sites you can have your server check for the browser version number in the HTTP request header and do the XSLT transformation server side only for non-XSLT “compliant browsers.

As time progresses and more and more people migrate to level-6 and higher browsers, Web servers will be increasingly able to offload XSLT processing to the client, thereby greatly enhancing the scalability of XML-based Web sites.

I l @ ve RuBoard

Extreme Programming for Web Projects
Extreme Programming for Web Projects
ISBN: 0201794276
EAN: 2147483647
Year: 2003
Pages: 95

Similar book on Amazon

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