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

RE: stylesheet functions or named templates

Subject: RE: stylesheet functions or named templates
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 21 Dec 2005 01:04:49 -0000
xsl function template
> Are there distinctions to be drawn that are not merely idiomatic or
> stylistic between a stylesheet function and a named template?

The main differences are:

* functions are called from XPath expressions, templates using call-template

* function arguments are specified by position and are always mandatory

* functions operate in an empty context, templates inherit a lot of context
from the caller

Generally, writing code as functions is a better way to make it reusable,
since function calls can be composed (combined) a lot more effectively than
template calls. But I continue to use templates when the main aim is to
create nodes and write them to a result tree.

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

> 
> That is, are there runtime considerations between a pair of 
> named templates
> like this:
> 
> 
>   <xsl:template name="correctDate">
>     <xsl:param name="dateParts"/>
>     <xsl:variable name="tokens" 
> select="fn:tokenize($dateParts, '/')"/>
>     <xsl:value-of select="$tokens[3]"/>
>     <xsl:text>-</xsl:text>
>     <xsl:call-template name="pad">
>       <xsl:with-param name="digits" select="$tokens[1]"/>
>     </xsl:call-template>
>     <xsl:text>-</xsl:text>
>     <xsl:call-template name="pad">
>       <xsl:with-param name="digits" select="$tokens[2]"/>
>     </xsl:call-template>
>   </xsl:template>
> 
>   <xsl:template name="pad">
>     <xsl:param name="digits"/>
>     <xsl:choose>
>       <xsl:when test="fn:string-length($digits) = 1">
>         <xsl:value-of select="concat('0', $digits)"/>
>       </xsl:when>
>       <xsl:otherwise>
>         <xsl:value-of select="$digits"/>
>       </xsl:otherwise>
>     </xsl:choose>
>   </xsl:template>
> 
> 
> And this pair of user defined functions:
> 
>   <xsl:function name="sfn:correctDate" as="xs:date">
>     <xsl:param name="s"/>
>     <xsl:variable name="seq" 
> 	select="fn:tokenize($s, '/')"/>
>     <xsl:value-of 
> 	
> select="concat($seq[3],'-',sfn:pad($seq[1]),'-',sfn:pad($seq[2]))"/>
>   </xsl:function>
> 
>   <xsl:function name="sfn:pad">
>     <xsl:param name="digits"/>
>     <xsl:choose>
>       <xsl:when test="fn:string-length($digits) = 1">
>         <xsl:value-of select="concat('0',$digits)"/>
>       </xsl:when>
>       <xsl:otherwise>
>         <xsl:value-of select="$digits"/>
>       </xsl:otherwise>
>     </xsl:choose>
>   </xsl:function>
> 
> 
> Both contain template bodies, the tree representing the named 
> *correctDate*
> template is a few nodes bigger, perhaps.  I can call 
> functions from within
> XPath expressions.  Anything else to consider in terms of execution
> efficiency?
> 
> 
> TIA,
> 
> Mike
> 
> -----------------------------------
> Mike Haarman,
> XSL Developer,
> Internet Broadcasting Systems, Inc.

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.