14.1 A Literal Result Element Stylesheet

Tired of the same old stylesheet? You can try a literal result element stylesheet. The original design idea behind a literal stylesheet was to provide a simple subset of XSLT for non-programmers who want to write fill-in-the-blank HTML pages.

First, we need something to transform. The document scand.xml found in examples/ch14 will do fine:

<?xml version="1.0" encoding="UTF-8"?>     <europe>  <scandinavia>   <state>Finland</state>   <state>Sweden</state>   <state>Iceland</state>   <state>Norway</state>   <state>Denmark</state>  </scandinavia> </europe>

This document is just a short list of the five Scandinavian countries in northern Europe. The following simplified XML literal stylesheet, literal.xsl, transforms scand.xml:

<?xml version="1.0" encoding="UTF-8"?>     <scandinavia xsl:version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:for-each select="europe/scandinavia/state">   <country><xsl:value-of select="."/></country>   </xsl:for-each> </scandinavia>

There is no stylesheet element (I already mentioned that it would be absent in a literal stylesheet). You can't even find a template element. The reason why there are no template elements found in a literal stylesheet is because a literal stylesheet cannot contain any top-level XSLT elements. This means that elements such as template, import, include, and output aren't allowed in literal stylesheets.

A literal stylesheet, in fact, works like a single, implicit template element that matches the root node (/). Only instructions elements like for-each, value-of, and copy-of will work inside this implicit template. This stylesheet uses for-each to march through all the state nodes in the source tree.

An important trick to making a literal stylesheet work is to use the xsl:version attribute on the document element together with the namespace declaration for XSLT using xmlns:xsl. With the xsl:version attribute and the XSLT namespace declaration in place, an XSLT processor will know how to interpret instruction elements from the XSLT namespace that it may find later in the document.

To see how it works, transform scand.xml with the literal stylesheet using the following command line:

xalan -i 1 scand.xml literal.xsl

This transformation will give you this result:

<?xml version="1.0" encoding="UTF-8"?> <scandinavia>  <country>Finland</country>  <country>Sweden</country>  <country>Iceland</country>  <country>Norway</country>  <country>Denmark</country> </scandinavia>

14.1.1 Pull and Push Stylesheets

Because they are limited by having only one template, literal stylesheets are reserved for pull transformations. A pull stylesheet might have one template rule that grabs input from the source tree with value-of or for-each in the order that is declared in the stylesheet, regardless of input order, but it will have few, if any, other template rules. A push stylesheet, on the other hand, uses lots of template rules and apply-templates so that the order of input, rather than the stylesheet, determines the order of the output.

It's hard to get apply-templates to work in a literal stylesheet. You can't use the select or mode attributes to select another template, because you have only one template (the implicit one that matches /). If you use apply-templates without an attribute, the literal stylesheet will process all the children of the root node, which may not be what you want. As a general rule, then, I suggest avoiding apply-templates in literal stylesheets.

Now on to an XHTML literal stylesheet that you can easily see in a browser.

14.1.2 A Literal XHTML Stylesheet

The literal stylesheet xhtmlit.xsl has the html element as its document element:

<?xml version="1.0" encoding="UTF-8"?>     <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns="http://www.w3.org/1999/xhtml"> <body> <h3>Scandanavian Countries</h3>  <ul>   <xsl:for-each select="europe/scandinavia/state">   <li><xsl:value-of select="."/></li>   </xsl:for-each>  </ul> </body> </html>

This stylesheet uses xsl:version and declares the XSLT namespace in its document element. It also declares the XHTML namespace as the default namespace (with no prefix). After some common XHTML elements, the stylesheet uses for-each to print list items from the state nodes in the source.

xhtmlit.xsl will not display directly in a browser; you have to transform it into regular HTML before it will display properly.


Process scand.xml with this stylesheet using the following:

xalan -i 1 -o scand.html scand.xml xhtmlit.xsl

This saves the file scand.html in the local directory. It looks like this:

<?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml">  <body>   <h3>Scandanavian Countries</h3>   <ul>    <li>Finland</li>    <li>Sweden</li>    <li>Iceland</li>    <li>Norway</li>    <li>Denmark</li>   </ul>  </body> </html>

Figure 14-1 shows you how scand.html looks in the Netscape browser.

Figure 14-1. scand.html in the Netscape browser
figs/lxsl_1401.gif


Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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