Figure 4-6. XSLT conceptual model
One weakness common to every approach other than XSLT is the
HTML-centric viewpoint. In every example presented thus far, it was
assumed that we generated HTML. What happens when the requirement to
support cellular phones arises? It is very likely that these devices
will not use HTML. Instead, they will use WML, XHTML Basic, or some
other technology that has not been invented yet. For now, consider
that you would have to write brand new servlets or JSPs to support
these devices when using traditional approaches. Any programming
logic embedded into JSP pages would be duplicated or would have to be
factored out into common helper classes. In a pure servlet approach,
the hardcoded HTML generation logic would have to be completely
rewritten.
XSLT offers an elegant solution -- simply create a second
stylesheet. Instead of transforming XML into HTML, this new
stylesheet transforms XML into WML. You can even support different
web browsers with the XSLT approach. Again, just write different
stylesheets for browser-specific functions. Since XSLT stylesheets
can import and include functionality from other stylesheets, much of
the code can be shared and reused across a project.
You might be wondering why this JDOM code is that much better than
the servlet code, which also used Java to programmatically produce
output. The difference is fundamental and important. With this JDOM
example, println( ) statements are not used.
Instead, a data structure representing the television schedule is
created. By virtue of the JDOM API, the data structure is guaranteed
to produce well-formed XML. We could very easily add a DTD, writing a
unit test that validates the integrity of the generated data
structure.
In addition to ensuring the integrity of the data, the JDOM code will
typically be much smaller than the servlet or JSP code. In this basic
web page, the servlet and JSP were quite small because the HTML did
not contain any significant formatting or layout. In a real-world web
page, however, the servlet and JSP will continue to grow in
complexity as the HTML layout gets more sophisticated, while the JDOM
code remains exactly the same.
NOTE:
Another interesting option made possible by this architecture is allowing the client to request raw XML without any kind of XSLT transformation. This allows your web site to support nonbrowser clients that wish to extract meaningful business data in a portable way.
We examined the weaknesses of other approaches, so it is only fair to
take a critical look at the XSLT approach. First, XSLT is a new
language that developers or web content authors have to learn.
Although the syntax is strange, it can be argued that XSLT is easier
to learn than a sophisticated programming language like Java. There
is resistance on this front, however, which is typical of a new
technology that is unfamiliar.
The second potential weakness of the XSLT approach is runtime
performance. There is a performance penalty associated with XSLT
transformation. Fortunately, there are numerous optimizations that
can be applied. The most common involves the caching of stylesheets
so they do not have to be parsed with each request. This and other
techniques for optimization will be covered in later chapters.
Since XSLT stylesheets are actually XML documents, any available XML
editor will work for XSLT. But eventually we should see more and more
specialized XSLT editors that hide some of the implementation details
for nonprogrammers. As with first-generation Java GUI builders, these
early tools may not generate stylesheets as cleanly as a handcoded
effort.