XML technologies / XSLT / Java and XSLT / Introduction / Java, XSLT and the web
(Excerpt from “Java und XSLT” by Eric M. Burke)
XSLT (eXtensible Stylesheet Language Transformations) was created to convert XML into other forms, usually HTML, XHTML or another XML format. An XSLT processor, such as Apache’s Xalan, performs transformations using one or more XSLT stylesheets, special XML documents. As the figure shows, XSLT runs in the application’s web tier while the client browser sees only HTML.
Figure: XSLT transformation
Java/XSLT web applications typically generate XML dynamically from database queries. Newer databases may export XML directly, but custom Java often reads data through JDBC and converts it. Data such as flight schedules or tax tables may require preparation for each consuming program. Most browsers need HTML, so the processor receives XML plus a stylesheet and streams the resulting HTML directly to the browser. The stylesheet supplies formatting while XML supplies raw data.
What is wrong with HTML?
A fundamental HTML problem is inconsistent implementation. Although W3C publishes specifications, competition between Netscape and Microsoft largely drove its development. Without coherent design and open standards, the language became bloated with browser-specific tags and inconsistent support. Since no two browsers support the same subset, authors restrict themselves to a common baseline or create separate pages per browser. Dynamic sites magnify these limitations when Java accesses enterprise data and prepares it for browsers.
Preparing enterprise data is much harder than building pages, requiring skilled programmers familiar with Enterprise JavaBeans or relational databases. Such scarce, costly people usually handle backend data and core functionality, while page developers and less experienced programmers build the HTML interface. As discussed under Java-based web technologies, this division can cause trouble with servlets because application logic and HTML generation are often mixed.
Separating data and presentation
HTML does not separate data from presentation. This fragment displays customer information, mixing fields such as “Stephan” and “Offenhammer” with formatting elements <tr> and <td>:
<h3>Kundeninformation</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr><td>Vorname:</td><td>Stephan</td></tr>
<tr><td>Nachname:</td><td>Offenhammer</td></tr>
<!-- etc... -->
</table>Such HTML is usually generated dynamically by a servlet’s println() statement, perhaps through a JavaServer Page. Both require Java programmers, and neither explicitly separates logic and data from HTML-generation code. Supporting incompatible browsers then requires care to avoid duplication, an unnecessary extra burden for programmers facing more important problems.
Application logic can be separated from HTML generation, but meaningful data is almost impossible to extract from HTML pages because HTML does not describe data structure. Humans infer field meanings, but writing an equally reliable program is hard. One can look for text patterns such as Vorname: followed by <td>. This screen-scraping approach fails as soon as presentation changes, for example to:
<tr><td>Name:</td><td>Stephan Offenhammer</td></tr>The XSLT solution
XSLT clearly separates roles: Java handles application logic, database queries and updates, and XML generation. XML holds raw data; XSLT converts it to browser-ready HTML. The main benefit is separating XML data from HTML presentation. To support multiple browsers, create several stylesheets using the same server data. The earlier example’s XML has no formatting instructions:
<kunde>
<vorname>Stephan</vorname>
<nachname>Offenhammer</nachname>
</kunde>Since XML contains only data, it is almost always clearer than HTML. It can also be generated through an API such as JDOM, making debugging and checking easier than emitting HTML as text through servlet PrintWriter and println() statements.
Best of all, write XML-generation code only once. Any number of stylesheets can reuse the XML for different browsers, multiple languages and even devices such as phones or PDAs.
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Java und XSLT” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de