Wendell Piez
The most important thing to understand about xsl:import is the notion of
"import precedence". You'll find this documented in any reasonably complete
guide to the language. Basically the idea is that a stylesheet's own
template rules override those in any imported stylesheet (in order based on
the sequence in which they are imported). This makes it easy to build a
modular arrangement of stylesheets in which the more generic rules are
provided by "lower-level" (imported stylesheets), and the more specific
rules belonging to a specific document type or subtype, or specific mode of
handling the documents, appear at the top-level.
So if your generic rules are in stylesheet A.xsl, you can have
A1.xsl imports A.xsl
A2.xsl imports A.xsl
where A1 and A2 each provide different "customization layers" over A.
So if you have scenarios A1, A2 and A3, instead of invoking A.xsl at runtime
and providing a parameter to distinguish between the cases, you simply
invoke A1.xsl, A2.xsl, or A3.xsl, whichever you need. Templates common to
all of them can be maintained in A.xsl; each of A1 - A3 can have the custom
code its processing scenario requires.
There are several advantages we get from this approach. For one, templates
in the different customization layers are easily distinguished; you don't
need special "case statements" in any templates, since the differentiation
between the cases has already been accomplished at the top level. The
customizing layers can each provide their own templates for special
handling, and don't have to knock elbows with the processing code belonging
to the other scenarios. And they can override the common layer more or less
drastically, depending.
Consider:
source document
<speech><sp>Miranda</sp>
<l>O, wonder!</l>
<l>How many goodly creatures are there here!</l> <l>How beauteous mankind
is! O <emph>brave new world</emph>,</l> <l>That has such people in't!</l>
</speech>
in A.xsl:
<xsl:template match="speech">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="sp"/>
<xsl:template match="emph">
<i>
<xsl:apply-templates/>
</i>
</xsl:template>
in A1.xsl: <xsl:import href="A.xsl"/>
<xsl:template match="l">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:template match="sp">
<h2 style="color:green">
<xsl:apply-templates/>
</h2>
</xsl:template>
in A2.xsl:
<xsl:import href="A.xsl"/>
<xsl:template match="l">
<h2>
<xsl:apply-templates/>
</h2>
</xsl:template>
in A3.xsl:
<xsl:import href="A.xsl"/>
<xsl:template match="l">
<h3>
<xsl:apply-templates/>
</h3>
</xsl:template>
If you run A1.xsl, A2.xsl or A3.xsl, the lines in the speech will come out
as h1, h2 or h3 depending, but in all of them, the speech will appear as a
<div>, and the emph will appear mapped to <i> (having matched the templates
in A.xsl). Additionally, the speaker ("Miranda") will appear in a green h2
-- but only from A1.xsl; in the others, it's suppressed. (A1.xsl provides a
better match for the 'sp' node due to its higher import precedence than
A.xsl; the others don't.)
I think you can probably extrapolate from this description to see how it
could apply in your case. I should think it would reduce the complexity of
your templates considerably, as well as making reuse and local customization
that much easier. Topics to research relating to this would include "import precedence",
xsl:import and xsl:apply-imports. |