[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: When/How to use templates, and when to use if/choo

Subject: Re: When/How to use templates, and when to use if/choose
From: "Ian Jolly" <Ian.Jolly@xxxxxxxxxxxxx>
Date: Wed, 20 Jun 2007 07:02:57 +0100
Re:  When/How to use templates
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@xxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, June 19, 2007 11:55 PM
Subject: RE: 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/



--
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.