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

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

Usage

The rules for

are so pervasive that one would imagine the facility is central to the use of XSLT, rather in the way inheritance is central to writing in Java. In practice, many stylesheets never need to use

, but you will almost certainly need it once you start to develop a family of stylesheets to handle a wide range of source document types.

Like inheritance in object-oriented languages,

is designed to allow the creation of a library or reusable components, only in this case, the components are modules of stylesheets. And the mechanism works in a very similar way to inheritance. For example, you might have a stylesheet that simply defines your corporate color scheme, as a set of global variables defining color names. Another stylesheet might be defined to produce the basic framesets for your site, referring to these color names to supply the background detail. Now if you want to use this general structure but to vary some detail, for example to modify one of the colors because it clashes with an image you are displaying on a particular page, you can define a stylesheet for this particular page that does nothing apart from redefining that one color. This is illustrated in
Figure 6-6
.

Suppose the stylesheet module for corporate color definitions looks like this:

       xmlns:color=“http://acme.co.nz/colors”

       version=“2.0”>





Now all the general-purpose stylesheets could

these definitions (without the need to

them unless they are being modified). This makes it easier to maintain the corporate brand image, because things are defined in one place only.

However, there are cases where you would want to depart from the general rule, and you can do so quite easily. If a particular document needs to use stylesheet C, but needs to vary the colors used, you can define stylesheet Z for it, as follows:

       xmlns:color=“http://acme.co.nz/colors”

       version=“1.0”>




In fact, this might be the entire stylesheet. In common English, style Z is the same as style C but with a different shade of lilac. Note that all the references to variable
color:lilac
are interpreted as references to the definition in Z, even if the references occur in the same stylesheet module as a different definition of
color:lilac
.

As a general principle, to incorporate standard content into a stylesheet without change, use

. If there are declarations you want to override, use

.

Examples

The first example is designed to show the effect of

on variables.

Example 1: Precedence of Variables

This example demonstrates the precedence of global variables when the principal stylesheet module and an imported module declare variables with the same name.

Source

This example can be run with any source XML file.

Stylesheet

The principal stylesheet module is
variables.xsl
.


    xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

    xmlns:acme=“http://acme.com/xslt”

    exclude-result-prefixes=“acme”>





  



The imported stylesheet module is
boilerplate.xsl
.


    xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

    xmlns:co=“http://acme.com/xslt”>

              select=“‘Acme Widgets Incorporated’”/>

              select=“concat(‘Copyright © ’, $co:company-name)”/>


Output

The output of this stylesheet will be:


Copyright © Acme Widgets Limited

This is because in the variable declaration of
$co:copyright
, the reference to variable
$co:company-name
matches the declaration of this variable in the principal stylesheet, because this has higher import precedence than the declaration in
boilerplate.xsl
.

The fact that different namespace prefixes are used in the two stylesheets is, of course, irrelevant: The prefix
acme
in the principal stylesheet maps to the same namespace URI as the prefix
co
in
boilerplate.xsl
, so the names are considered equivalent.

This example explicitly specifies
encoding = “iso-8859-1”
for both the stylesheet modules and the output. Most of my examples only use ASCII characters, and since the default character encoding UTF-8 is a superset of ASCII, this works fine. This time, though, I've used the copyright symbol
©
, which is not an ASCII character, so it's important to specify the character encoding that my text editor uses, which is iso-8859-1.

The second example shows the effect of

on template rules.

Other books

Unknown by Unknown
An Uninvited Ghost by E.J. Copperman
The Demon Side by Heaven Liegh Eldeen
Heart of a Dove by Abbie Williams
Eric S. Brown by Last Stand in a Dead Land
Special Delivery by Ann M. Martin
The Virginity Mission by Cate Ellink