Fill-in-the-Blanks Stylesheets


Many proprietary templating languages have been built up around HTML. The template looks largely like a standard HTML file, but with the addition of extra tags used to retrieve variable data and insert it at a particular point in the HTML data page. The designers of XSLT took care to ensure that in spite of the power of XSLT as a full transformation language, it would still be possible to use it in this simple way, bringing it within the reach of nonprogrammers with HTML authoring skills.

A "Fill-in-the-Blanks" Stylesheet
start example

Here's an example of such a stylesheet. It uses the simplified stylesheet syntax, so the <xsl:stylesheet> element and the <xsl:template match="/"> element are implicit.

Input

This XML document, orgchart. xml, represents an organization chart showing the senior management of a certain company at a particular date. It is organized as a recursive structure that directly reflects the management hierarchy. You may recognize the names , but the roles are entirely fictitious.

  <?xml version="1.0" encoding="iso-8859-1"?>   <orgchart date="2004-03-31">   <person>   <name>Tim Berners-Lee</name>   <title>Chief Executive Officer</title>   <reports>   <person>   <name>Sharon Adler</name>   <title>Technical Director</title>   <reports>   <person>   <name>Tim Bray</name>   <title>Chief Engineer</title>   </person>   <person>   <name>James Clark</name>   <title>Director of Research</title>   </person>   </reports>   </person>   <person>   <name>Henry Thompson</name>   <title>Operations and Finance</title>   </person>   <person>   <name>David Megginson</name>   <title>Human Resources</title>   </person>   <person>   <name>Steve Muench</name>   <title>Marketing</title>   </person>   <person>   <name>Scott Boag</name>   <title>International</title>   </person>   </reports>   </person>  

Stylesheet

There are many creative ways to display this data; for example, you could use SVG graphics, explorer-style trees implemented in client-side JavaScript, or just indented lists. I'm not trying to teach you any clever HTML tricks, so in this stylesheet ( orgchart.xsl ) I'll show the data instead as a rather boring table, with one row per person, and three columns for the person's name, their title, and the name of their boss.

  <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xsl:version="2.0">   <head>   <title>Management Structure</title>   </head>   <body>   <h1>Management Structure</h1>   <p>The following responsibilities were announced on   <xsl:value-of select="format-date(/orgchart/@date,   '[D1] [MNn] [Y1]')"/>:</p>   <table border="2" cellpadding="5">   <tr>   <th>Name</th><th>Role</th><th>Reporting to</th>   </tr>   <xsl:for-each select="//person">   <tr>   <td><xsl:value-of select="name"/></td>   <td><xsl:value-of select="title"/></td>   <td><xsl:value-of select="ancestor::person[l]/name"/></td>   </tr>   </xsl::for-each>   </table>   <hr/>   </body>   </html>  

The key to this design pattern is that the stylesheet has the same structure as the desired output. Fixed content is included directly in the stylesheet as text or as literal result elements, while variable content is included by means of <xsl:value-of > instructions that extract the relevant data from the source document. Repeated sections of output, typically rows in a table or items in a list, can be enclosed by <xsl:for-each> , and conditional sections by <xsl:if>or<xsl:choose> .

Output

The output of this stylesheet is shown in Figure 9-1.

click to expand
Figure 9-1
end example
 

This kind of stylesheet makes very limited use of XSLT's power, but it is very similar to a wide variety of proprietary templating languages currently in use. Experience has shown that this kind of stylesheet is easy for experienced HTML authors to write, even if they have no programming training. This is an important consideration, because on many larger Web sites there is a constant need to introduce new page templates at very short notice, and this becomes much easier to achieve if content authors and editors can do the work themselves .

One restriction, of course, is that the input has to come from an XML document. This contrasts with most of the proprietary languages, where the input often comes directly from a relational database. Fortunately all popular relational databases now provide convenient ways to extract data from a database in XML form. Ideally, this doesn't even need to be a serial XML document that has to be re-parsed by the XSLT processor. It will often be possible to transfer the data directly from the database to the XSLT processor in a structured form, for example as a DOM tree in memory or as a SAX event stream. The details of how to do this depend on the database product you are using, and are beyond the scope of this book.

Another approach is to use the document() function (described in Chapter 7, page 532) with a URI that addresses a servlet with parameters to retrieve the required data.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

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