Re: [xsl] misc. confusion on "footnote" handling

Subject: Re: [xsl] misc. confusion on "footnote" handling
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 25 Aug 2004 18:19:29 -0400
Bruce,

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.

Cheers,
Wendell

At 04:44 PM 8/25/2004, you wrote:
Incidentally, another design pattern of which you might not be aware is to place most of your code in a common core, but have different stylesheets to invoke for running in different "modes".

This sounds interesting. Could you give me an example of what this might look like?


Currently I have a main stylesheet that looks just like this:

<!-- read the external citation style file -->
<xsl:param name="citation-style" required="yes" as="xs:string" />

<xsl:variable name="styles" as="document-node()"
  select="doc(concat('styles/',$citation-style, '.csl'))" />

<!-- set the citation class parameter (e.g. author-year) as specified in the style file -->
<xsl:param name="citation-class" select="$styles/cs:citationstyle/@class"/>


<xsl:include href="drivers/biblio-driver.xsl" />
<xsl:include href="drivers/document-driver.xsl" />
<xsl:include href="output/biblio-output.xsl" />
<xsl:include href="output/citation-output.xsl" />

Bruce


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread