xsl-list
[Top] [All Lists]

Re: [xsl] When/How to use templates, and when to use if/choose

2007-06-19 23:03:34
What happens in terms of performance? One assumes that there will be some sort of overhead with using templates, and then later on templates with modes - is this overhead significant, or do I have bigger things to worry about?

Thanks,

Ian
----- Original Message ----- From: "Michael Kay" <mike(_at_)saxonica(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Tuesday, June 19, 2007 11:55 PM
Subject: RE: [xsl] When/How to use templates, and when to use if/choose


Using templates sometimes seems to be more of a pain than not!

Well, this first example certainly seems to read much better with templates:

<xsl:template match="doc:Body//doc:Para">
 <xsl:number count="doc:Para" from="doc:Body" level="any"/>
 <xsl:text>. </xsl:text>
 <xsl:call-template name="prefix"/>
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="doc:appendix//doc:Para">
 <font xsl:use-attribute-sets="attSetAppendixBody">
   <xsl:call-template name="prefix"/>
   <xsl:apply-templates/>
 </font>
</xsl:template>

<xsl:template match="doc:Para">
 <xsl:call-template name="prefix"/>
 <xsl:apply-templates/>
</xsl:template>

What are the benefits? Well, it's a bit shorter, and it's a bit more
readable, but the main benefit is reusability of code and potential for
change. An importing stylesheet can override any one of these templates
without being concerned with the others, or it can add extra templates that
select a different subset of the doc:Para elements. As soon as you want to
create a suite of stylesheets that do a number of different jobs, or process
a variety of different inputs, that becomes invaluable.

Also,

<xsl:template match="doc:title">
    <xsl:choose>
        <xsl:when test="parent::doc:Table">
            <xsl:apply-templates/>
        </xsl:when>
        <xsl:when test="ancestor::doc:Summary">
            <xsl:choose>
                <xsl:when test="../../../../doc:Section">

This is starting to look pretty hideous; it certainly feels to me that rules
of the form

<xsl:template match="doc:Section//doc:Summary/doc:Table/doc:title">

are vastly preferable. But you don't have to do everything with templates:
looking at the above, without knowing anything about your problem, I would
probably be inclined to process most of the title elements from their
containing element, for example

<xsl:template match="doc:Table">
 <p><xsl:value-of select="doc:title"></p>

rather than having a rule for titles that worries about all the possible
places they can appear.

Michael Kay
http://www.saxonica.com/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.1/854 - Release Date: 19/06/2007 13:12




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--