Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Earlier releases of AltovaXML had incomplete support for the
format-date()
function: the date was output, but not in the requested format. This is fixed in the 2008 release.
copyright.xsl
Finally, the module
copyright.xsl
contains a named template that outputs a copyright statement. This template is called by the
$owner
to construct the copyright statement; we'll see later how this is useful.
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0”>
The reason for separating this stylesheet program into three modules is that the
date.xsl
and
copyright.xsl
modules are reusable in other stylesheets. Functionally, the stylesheet would have exactly the same effect if the variable
$date
and the template named
copyright
were defined directly in the principal stylesheet module.
Output
There is no syntactic difference between a principal stylesheet module and any other module; in fact any module can be used as a principal module.
This means that
A stylesheet module is generally a complete XML document (the exception, an
embedded stylesheet
, will be described later on page 102). The document element (the outermost element of the XML document) is then either an
declarations
. The XSLT-defined declarations are listed on page 105.
The
href
attribute whose value is a URI. Most commonly, it will be a relative URI, defining the location of the included or imported stylesheet module relative to the parent module. For example,
mod1.xsl
located in the same directory as the parent module.
The difference between
It may not come naturally to think of the importing module as a subclass of the imported module, because in a class hierarchy, the most general classes are near the root of the tree, whereas in the
The most common kind of declaration is the definition of a template rule, using an
match
attribute. As we saw in the previous chapter, if there are several template rules that match a particular node in the source tree, the first step in deciding which to use is to look at their import precedence, and discard all those with import precedence less than the highest. So a template rule defined in a particular stylesheet module will automatically take precedence over another matching rule in a module that it imports.
Where one module A imports two others, B and C, as shown in
Figure 3-1
, then A takes precedence over both B and C, and C also takes precedence over B, assuming that the
When a stylesheet incorporates another using
Where two declarations have the same import precedence (because they were in the same stylesheet module, or because one was in a module incorporated in the other using
Example: Using
This extends the previous
Source
The input document for this example is
sample.xml
.
Stylesheet
Recall that the
copyright.xsl
module used a variable,
$owner
, to hold the name of the copyright owner. Suppose that we want to use the
copyright
template, but with a different copyright owner. We can achieve this by writing a revised principal stylesheet as follows (this is called
principal2.xsl
in the downloadable sample files).
This stylesheet uses
copyright.xsl
module, and it then contains a new declaration of the
$owner
variable, which will override the declaration in the imported module. Note that the
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0”
>
Output
This example wouldn't work if you used
$owner
was declared twice. This is because with
It is an error for a stylesheet module to import or include itself, directly or indirectly, because doing so would define an infinite loop.
It isn't an error, however, for a stylesheet module to be included or imported at more than one place in the stylesheet program. The following isn't an error.
This may seem rather pointless, but in a highly modular structure it can sometimes happen by accident and be harmless. For example, several of your stylesheet modules might independently reference a commonly used module such as
date.xsl
. The effect is simply to load two copies of all the declarations in
date.xsl
, exactly as if two identical files with different names had been imported.
If the same module is fetched twice using
Note that with both
href
attribute is fixed: it is possible for a stylesheet compiler to assemble all the modules in a stylesheet well before a source document is supplied to actually run the transformation. People often ask for some kind of facility to load stylesheet modules dynamically, based on a decision made while the transformation is running. The simple answer is that you can't do this: you have to construct the whole stylesheet before you can start running it.
Sometimes, this requirement arises when people try to use