tags:

views:

39

answers:

1

Which is better practice to use for execution flow, call-template or modes?

data.xml

<Properties>
    <foo>me</foo>
    <bar>you</bar>
</Properties>

a.xsl

<xsl:include href="translations_nomodes.xml"
<xsl:template match="/">
    <xsl:call-template name="a_display"/>
</xsl:template>

b.xsl

<xsl:include href="translations_nomodes.xml"
<xsl:template match="/">
    <xsl:call-template name="b_display"/>
</xsl:template>

translations_nomodes.xsl

<xsl:template name="a_display">
    <!-- display option a -->
    ...
</xsl:template>

<xsl:template name="b_display">
    <!-- display option b -->
    ...
</xsl:template>

Or would using modes be a better practice

c.xsl

<xsl:include href="translations_modes.xml"
<xsl:template match="/">
    <xsl:apply-templates select="/Properties" mode="c_display"/>
</xsl:template>

d.xsl

<xsl:include href="translations_modes.xml"
<xsl:template match="/">
    <xsl:apply-templates select="/Properties" mode="d_display"/>
</xsl:template>

translations_modes.xsl

<xsl:template match="Properties" mode="c_display">
    <!-- display option c -->
    ...
</xsl:template>

<xsl:template match="Properties" mode="d_display">
    <!-- display option d -->
    ...
</xsl:template>

Since "Properties" is the root node in my document and the apply-templates use literals for their mode values, using mode won't give me any added benefit and it's slightly more verbose. However if the execution flow is dependent upon an element/attribute within the document itself and the modes were not literals but expressions, then I could see the need for the mode approach.

In fact, using modes as I am, with literal values, seems like a bad choice also because if down the road my logic changes and I need to use mode expressions to control execution flow, I've already 'used' the mode attribute.

Have I come to the correct conclusion or am I missing some important points?

A: 

Remember: XSLT (as any declarative language) binds an input with wellknown schema to an output with wellknown schema.

There is no such thing as "general solution".

Looking your example, this are the facts:

  • Your input source has a Properties root element.
  • Your process logic doesn't rest in XSLT, but in your decision: to run one transformation or another.

So, why did you have a common stylesheet with separate logic, and two "entry point" stylesheets for each logic? This doesn't make sense.

Best practice would be to have two stylesheet.

Alejandro
why are there two 'entry point" style sheets? The skinning framework that I am working within requires it. Why is there a common stylesheet? I didn't include it in my example but often times the skins share common processing logic before branching. So your recommendation would be to remove the branching logic from the common stylesheet and just include the common stuff in each unique stylesheet?
new Thrall
@new Thrall: I'm telling you what I see. `translations_nomodes.xsl` and `translations_modes.xsl` are the common stylesheets with diferent process logic trough named templates or modes. `a.xsl` and `b.xsl` or `c.xsl` and `d.xsl` are just entry point. Maybe you really have some common rules, but that is no showed here. You are driving the process logic, not the processor.
Alejandro