Re: [xsl] Rescuing XSLT from Niche Status

Subject: Re: [xsl] Rescuing XSLT from Niche Status
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 17 Feb 2001 13:41:23 +0000
Hi David,

> I still think that it is helpful for beginners to think of the '/'
> template as the "layout" or "controller" template. Again remember my
> web application / HTML focus not real complex XML documents. I have
> been convinced though that once a person understands how to use the
> '/' template it would be useful to quickly migrate them towards
> moving element formatting into their own templates using modes as
> you suggest, while keeping the '/' template for controlling the
> overall layout/organization of the document. I think this maintains
> the smooth learning curve I trying to achieve.

I like the 'top-level template as controller' approach too, although I
tend make the distinction between the overall control - what bits of
the source should be processed, say, or in what mode - and production
of output. Control of *output* - the overall structure of the document
- I tend to put in a template matching the document element. So
typically I might have:

<!-- this is a copy of the built-in template for the root node -->
<xsl:template match="/">
   <xsl:apply-templates />

<!-- this template actually produces something -->
<xsl:template match="/*">

The reason for this is for extensibility. If I now want to add another
set of templates to produce WML rather than HTML and use a parameter
to choose between them, I can just change the root-node-matching
template to:

<xsl:template match="/">
      <xsl:when test="$method = 'wml'">
         <xsl:apply-templates mode="wml" />
      <xsl:otherwise><xsl:apply-templates /></xsl:otherwise>

Similarly, if I want to pre-process the data to filter out something,
I can adjust the root-node-matching template:

<xsl:template match="/">
   <xsl:variable name="processed">
      <xsl:apply-templates mode="filter" />
   <xsl:apply-templates select="$processed/*" />

I find it also makes it easier to later combine stylesheets, but
probably that's not particularly high on the web-application priority

If you like, there's a more natural 'match' between matching the
document element of the source and generating the document element of
the result than there is between the root node of the source and the
document element of the result.



Jeni Tennison

 XSL-List info and archive:

Current Thread