Subject: [xsl] Calling templates with (defaulted) params From: "Ruggier, Mario" <Mario.Ruggier@xxxxxxxxxxxxxxxx> Date: Thu, 31 May 2001 17:52:18 +0200 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] excluding xml elements/co, Oliver Becker | Thread | RE: [xsl] Calling templates with (d, Michael Kay |
RE: [xsl] String to number, Michael Kay | Date | RE: [xsl] Chris Bayes tutorial: xsl, Chris Bayes |
Month |