Re: [xsl] mode computation case

Subject: Re: [xsl] mode computation case
From: ac <ac@xxxxxxxxxxxxx>
Date: Tue, 31 Aug 2010 07:08:36 -0400
Hi Michael,

Interesting idea.

My use-case is about processing a highly structured XML document in many different ways, including for various types of transformations, calculations, statistics, reporting, and presentation, as the document is maintained and evolves, hence the use of different modes, typically selected, along with a set of parameter values, from user input, for example. For a more concrete example, the document could be a model from a building, or a car, or anything one may wish to model as a structured XML document, ideally, the universe;). I see this pattern as quite basic and fundamental. I also feel that it needs to match on the nodes in the different modes rather than on the modes in different nodes.

What do you think?

Regards,
Andre



I'm sure one can devise ways of using this feature, but the real question is whether there are already acceptable ways of solving the user-level problem. To answer that question, we need a high-level description of the transformation you want to carry out.

One alternative that comes to mind is to do

<xsl:apply-templates select="@mode">
<xsl:with-param name="nodetree" select="$nodetree"/>
  ...
</

and then have template rules

<xsl:template match="@mode[.=mode1]">
  ...
</xsl:template>

etc.

On 31/08/2010 5:27 AM, ac wrote:
Hi,

I have something like

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


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

Current Thread