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

RE: Calling templates with (defaulted) params

Subject: RE: Calling templates with (defaulted) params
From: "Michael Kay" <mhkay@xxxxxxxxxxxx>
Date: Thu, 31 May 2001 17:08:31 +0100
defaulted value
Specify <xsl:param name="x" value="default-value-here"/>

Mike Kay
Software AG

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Ruggier,
> Mario
> Sent: 31 May 2001 16:52
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Calling templates with (defaulted) params
> 
> 
> Hi, 
> 
> in an XSLT that processes an XML description to produce HTML,
> I would like to be able to define any number of small "rendering"
> templates 
> for logical elements that I wish to define. To make the templates
> reusable,
> they are parametrized. The question is the number of parameters may be
> long, and having to state a value for each one each time the 
> template is
> called is not convenient. Is it possible to reduce overheaviness (i.e.
> not replace
> it by something just as heavy somewhere else in the code ;-) 
> by having 
> each template decide default values for each parameter, and uses it in
> each 
> case a parameter is not specified ?
> 
> In the small example below, imagine a logical "section", 
> represented by
> the <icm key="Section">, and a single param. This is processed by the 
> proposed XSLT, to call a template called "Section" that expects 2
> params.
> Imagine that the non-specified param should assume some value, 
> e.g. 'no value for note heading!'. Two ways of calling the template
> "Section"
> are included below, one with no defaults, and one with defaults, which
> is significantly heavier (and this is an example with only 2 params!).
> 
> My question is, are there other lighter ways to do this in XSLT?
> Any improvements anyone can suggest? 
> Any optimization should be in favour of many such templates, each 
> with possibly many parameters. And, any shortcuts should also favour
> the source xml description rather than the xsl that processes it.  
> 
> tia, and best regards, 
> 
> Mario Ruggier
> 
> 
> ==== xml source ====
> ...
> <icm key="Section">
>         <param name="heading" value="Section Heading" />
>         ....
> </icm>
> ...
> 
> 
> ===== xsl source ====
> 
> <xsl:output method="html" indent="yes" />
> 
> <!-- icm template wrapper to call templates according to @key 
> value -->
> <xsl:template match="icm">
>   <xsl:variable name="icm-key" select="@key" />
>   <xsl:choose>
>     ....
>     <xsl:when test="$icm-key='Section'">
> 
>       <!-- no defaulting... --> 
>       <xsl:call-template name="Section" >
>         <xsl:with-param name="heading"
> select="param[@name='heading']/@value" />
>         <xsl:with-param name="headingNote"
> select="param[@name='headingNote']/@value" />
>        </xsl:call-template>
> 
>       <!-- with defaulting... --> 
>       <xsl:call-template name="Section" >
>          <xsl:with-param name="heading">
>             <xsl:call-template name="get-defaulted-value">
>                 <xsl:with-param name="proposed-value"
> select="param[@name='heading']/@value" />
>                 <xsl:with-param name="default-value" select="'no value
> for heading!'" />
>             </xsl:call-template>
>          </xsl:with-param>
>          <xsl:with-param name="headingNote" 
>            <xsl:call-template name="get-defaulted-value">
>                 <xsl:with-param name="proposed-value"
> select="param[@name='headingNote']/@value" />
>                 <xsl:with-param name="default-value" select="'no value
> for heading note!'" />
>             </xsl:call-template>
>          </xsl:with-param>
>        </xsl:call-template>
> 
>     </xsl:when>
>     ...
>   </xsl:choose>
> </xsl:template>
> 
> ... <!-- other templates .--> 
> 
> <xsl:template name="Section" >
>   <xsl:param name="heading" />
>   <xsl:param name="headingNote" />
>     <table border="0" cellspacing="2" cellpadding="2" >
>       <tr valign="top" align="left">
>         <th bgcolor="#99ffff"><xsl:value-of select="$heading" /></th>
>         <td><xsl:value-of select="$headingNote" /></td>
>       </tr>
>       <xsl:apply-templates />
>     </table>
> </xsl:template>
> 
> ... 
> 
> <!-- "defaulting" function --> 
> <xsl:template name="get-defaulted-value">
>   <xsl:param name="proposed-value" />
>   <xsl:param name="default-value" />
>   <xsl:choose>
>     <xsl:when test="$proposed-value"><xsl:value-of
> select="$proposed-value" /></xsl:when>
>     <xsl:otherwise><xsl:value-of select="$default-value"
> /></xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
> 
> 
> ========
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.