xsl-list
[Top] [All Lists]

Re: [xsl] mode computation case

2010-09-02 21:16:23
 Hi Michael, Wendell, David,

As a note, by separating preparation and dispatch, and assuming that all parameters for all modes are tunneled and common, the following:

<xsl:template name="prepare-and-dispatch">
...
<xsl: variable name="var1" select="...some value or node-set generating expression"/> <xsl: variable name="var1" select="...some other value or node-set generating expression"/> <xsl: variable name="var1" select="...some other other value or node-set generating expression"/> <xsl: variable name="var1" select="...another value or node-set generating expression"/>
<xsl:choose>
<xsl:when test="@mode eq 'mode1'">
<xsl:apply-templates select="$nodetree" mode="mode1">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="@mode eq 'mode2'">
<xsl:apply-templates select="$nodetree" mode="mode2">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="@mode eq 'mode3'">
<xsl:apply-templates select="$nodetree" mode="mode3">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="@mode eq 'mode4'">
<xsl:apply-templates select="$nodetree" mode="mode4">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="@mode eq 'mode5">
<xsl:apply-templates select="$nodetree" mode="mode5">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="@mode eq 'mode6'">
<xsl:apply-templates select="$nodetree" mode="mode6">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$nodetree" mode="modeX">
<xsl:with-param name="param1" tunnel="yes" select="$var1"/>
<xsl:with-param name="param2" tunnel="yes" select="$var2"/>
<xsl:with-param name="param3" tunnel="yes" select="$var3"/>
<xsl:with-param name="param4" tunnel="yes" select="$var4"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
...
</xsl:template>


can be reduced to:

<xsl:template name="prepare">
    ...
<xsl:call-template name="dispatch">
<xsl:with-param name="param1" tunnel="yes" select="...some value or node-set generating expression"/> <xsl:with-param name="param2" tunnel="yes" select="...some other value or node-set generating expression"/> <xsl:with-param name="param3" tunnel="yes" select="...some other other value or node-set generating expression"/> <xsl:with-param name="param4" tunnel="yes" select="..another value or node-set generating expression"/>
</xsl:call-template>
    ...
</xsl:template>

<xsl:template name="dispatch">
    ...
<xsl:choose>
<xsl:when test="@mode eq 'mode1'"><xsl:apply-templates select="$nodetree" mode="mode1"></xsl:when <xsl:when test="@mode eq 'mode2'"><xsl:apply-templates select="$nodetree" mode="mode2"></xsl:when <xsl:when test="@mode eq 'mode3'"><xsl:apply-templates select="$nodetree" mode="mode3"></xsl:when <xsl:when test="@mode eq 'mode4'"><xsl:apply-templates select="$nodetree" mode="mode4"></xsl:when <xsl:when test="@mode eq 'mode5'"><xsl:apply-templates select="$nodetree" mode="mode5"></xsl:when <xsl:when test="@mode eq 'mode6'"><xsl:apply-templates select="$nodetree" mode="mode6"></xsl:when <xsl:otherwise><xsl:apply-templates select="$nodetree" mode="modeX"></xsl:otherwise>
</xsl:choose
    ...
<xsl:template>


Which already looks better, mostly since all modes in this example share the same tunneled parameters. The xsl:choose in "dispatch" is still somewhat clunky and an additional template is required.
Therefore, I still propose that mode be made computable.

Regards,
ac




And there is here only 7 modes and 4 parameters, instead, if the apply-templates could be computed, I would have liked to possibly have something less redundant, for any number of modes and parameters, more like
...
<xsl:apply-templates select="$nodetree" mode="@mode">
<xsl:with-param name="param1" tunnel="yes" select="...some value or node-set generating expression"/> <xsl:with-param name="param2" tunnel="yes" select="...some other value or node-set generating expression"/> <xsl:with-param name="param3" tunnel="yes" select="...some other other value or node-set generating expression"/> <xsl:with-param name="param4" tunnel="yes" select="..another value or node-set generating expression"/>
</xsl:apply-templates>
...

It seems that it could save space, variables, design time, run time, typos, errors, as well as ease maintenance and update (e.g adding mode7 and forgetting to add another xsl:when clause for it ...), while increasing readability.

Are there more elegant ways to handle mode selection? Can this be a useful use-case to support allowing "mode" to be computed somehow, in future XSLT updates?

Thank you,
ac


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



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

<Prev in Thread] Current Thread [Next in Thread>