XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (53 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.27Mb size Format: txt, pdf, ePub
ads

Example: A Simplified Stylesheet

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 under the filename
simplified.xsl
, and can be used together with the data file
books.xml
.

The complete stylesheet is as follows:

      xsl:version=“2.0”>

A list of books


A list of books

   

   

      

      

        

        

        

        

      

   

   



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

A simplified stylesheet is equivalent to a stylesheet in which the outermost element (typically the

element) is wrapped first in an

element with
match = “/”
, and then in an

element. The
xsl:version
attribute of the outermost element becomes the
version
attribute of the

. So the expanded form of the above example would be as follows:

                version=“2.0”>



A list of books


A list of books

   

   

      

      

        

        

        

        

      

   

   





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

and

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.

Writing Portable Stylesheets

In this section we will examine a range of facilities that are included in XSLT to help you write portable stylesheets; that is, stylesheets that can run across different XSLT processors, possibly supporting different versions of the XSLT language.

We will look at the question of version compatibility; that is, how to write stylesheets that work with both XSLT 1.0 and XSLT 2.0. Then we will look at how to use vendor extensions, without sacrificing portability. But before we do either of these things, I will describe a new feature that has been added to XSLT 2.0 to aid portability, namely the
use-when
attribute, which allows you to include or exclude stylesheet code conditionally at compile time.

Conditional Compilation

The
use-when
attribute serves a similar purpose to
#ifdef
in the C language: it allows you to define conditions under which a section of the stylesheet can be conditionally included or excluded at compile time.

The
use-when
attribute can be used on any XSLT element. This includes declarations and instructions, and other elements such as

and

. Written as
xsl:use-when
, it is also allowed on literal result elements and extension instructions. The value of the attribute is a condition to be evaluated at compile time. If the condition is false, then the element and the subtree rooted at that element are effectively eliminated from the stylesheet, before any further processing takes place: it is as if the element and its content were not there. One consequence is that no XSLT errors will be reported in respect of this element or its descendants.

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.27Mb size Format: txt, pdf, ePub
ads

Other books

Triple Shot by Sandra Balzo
The Unveiling by Tamara Leigh
The Dangerous Lord by Sabrina Jeffries
A fine and bitter snow by Dana Stabenow
The Destroyer Book 3 by Michael-Scott Earle
Blasfemia by Douglas Preston
Wicked Highlander by Donna Grant
Pretty When She Dies by Rhiannon Frater
Beloved Texas Bride by Ginny Sterling