Simplified Stylesheets


A simplified stylesheet uses an abbreviated syntax in which the <xsl:stylesheet> element and all the top-level declarations are omitted.

The original purpose of this facility was to allow people with HTML-authoring skills but no programming experience to write simple stylesheets with a minimum of training. A simplified stylesheet has a skeleton that looks like the target document (which is usually HTML, though it doesn't have to be), and uses XSLT instructions to fill in the variable parts .

A stylesheet module is interpreted as a simplified stylesheet if the outermost element is not <xsl:stylesheet> or <xsl:transform>. The outermost element can have any name , provided it is not in the XSLT namespace. It must still contain a declaration of the XSLT namespace, and it must have an xsl:version attribute. For XSLT 2.0 the value should be 1.0 ‰« or 2.0 ‰« (use 2.0 ‰« if your stylesheet depends on features defined in XSLT 2.0, or 1.0 ‰« if you also want it to work with XSLT 1.0 processors). When the xsl:version attribute is greater than 2.0 ‰« , forwards-compatible processing mode is enabled. This is discussed later in this chapter on page 124.

A Simplified Stylesheet
start example

This example shows a stylesheet that takes the form of an HTML skeleton page, with XSLT instructions embedded within it to pull data from the source document. The stylesheet is in the download file available from http://www.wrox.com/ . It has the filename simplified.xsl , and can be used together with the data file books.xml .

The complete stylesheet is as follows .

  <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xsl:version="1.0">   <head><title>A list of books</title></head>   <body>   <h1>A list of books</h1>   <table border="2">   <xsl:for-each select="//book">   <xsl:sort select="author"/>   <tr>   <td><xsl:value-of select="author"/></td>   <td><xsl:value-of select="title"/></td>   <td><xsl:value-of select="@category"/></td>   <td><xsl:value-of select="price"/></td>   </tr>   </xsl:for-each>   </table>   </body>   </html>  

When you run this against the file books.xml (which is listed on page 66 in Chapter 2), the output is a sorted table showing the books (Figure 3-6).

click to expand
Figure 3-6
end example
 

A simplified stylesheet is equivalent to a stylesheet in which the outermost element (typically the <html> element) is wrapped first in an <xsl:template> element with match=" /" ‰« , and then in an <xsl:stylesheet> element. The xsl:version attribute of the outermost element becomes the version attribute of the <xsl:stylesheet> . So the expanded form of the above example would be as follows.

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">   <xsl:template match="/">   <html>   <head><title>A list of books</title></head>   <body>   <h1>A list of books</h1>   <table border="2">   <xsl:for-each select="//book">   <xsl:sort select="author"/>   <tr>   <td><xsl:value-of select="author"/></td>   <td><xsl:value-of select="title"/></td>   <td><xsl:value-of select="@category"/></td>   <td><xsl:value-of select="price"/></td>   </tr>   </xsl:for-each>   </table>   </body>   </html>   </xsl:template>   </xsl:stylesheet>  

The significance of match="/" ‰« is that this identifies the template rule as the first one to be processed when the stylesheet is activated. As we saw in Chapter 2, processing generally starts at the root node of the source document tree, and whichever template rule matches this root node is the first one to be invoked. The match pattern / ‰« matches a document node. In a simplified stylesheet, this will be the only template rule invoked.

There are many things a simplified stylesheet cannot do, because it cannot contain any declarations. For example, a simplified stylesheet can't include or import another stylesheet, it can't have global variables or parameters, and it can't define keys. But when you need these extra capabilities, you can always "unsimplify" the stylesheet by adding the surrounding <xsl:stylesheet> and <xsl:template> elements.

It is possible in theory for a stylesheet to include or import a simplified stylesheet, which would be expanded exactly as described above-but it would be a rather unusual thing to do.




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