Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Usage
The rules for
Like inheritance in object-oriented languages,
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
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
Examples
The first example is designed to show the effect of
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:
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