[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Data-driven XSLT code using dynamic mode values?

Subject: RE: [xsl] Data-driven XSLT code using dynamic mode values?
From: "Vladimir Nesterovsky" <vladimir@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 2 Aug 2013 18:08:13 +0300


You may define $action as element, and create a template with mode "call"
that will forward to a final destination.

<xsl:apply-templates mode="call" select="$action">
  <xsl:with-param name="data" select="."/>

<xsl:template mode="call" match="create">
  <xsl:param name="data" as="element()"/>

Vladimir Nesterovsky

-----Original Message-----
From: Costello, Roger L. [mailto:costello@xxxxxxxxx] 
Sent: Friday, August 2, 2013 5:27 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Data-driven XSLT code using dynamic mode values?

Hi Folks,

I want to have multiple templates for the same element, each template
processes the element differently.

"Use modes," you say.

Okay, sounds good. So I will have multiple template rules:
    <xsl:template match="Header" mode="Remove"> 
    <xsl:template match="Header" mode="Replace"> 
    <xsl:template match="Header" mode="Reject"> 

Now let's turn to the problem of firing the appropriate template rule. 

I have a variable which has a value that indicates whether I should do a
Remove, Replace, or Reject:

    <xsl:variable name="action" select="get the action from an XML file" />

I want to fire a template rule based on the value of $action. 

Here's what I really, really want to do:

    <xsl:apply-templates select="." mode="$action" />

That would be so cool. That would truly be "data-driven code."

Unfortunately, using a variable as the value of @mode is not allowed.


Is my only solution to use xsl:choose:

              <xsl:when test="$action eq 'Remove'">
                  <xsl:apply-templates select="." mode="Remove" />
              <xsl:when test="$action eq 'Replace'">
                  <xsl:apply-templates select="." mode="Replace" />
              <xsl:when test="$action eq 'Reject'">
                  <xsl:apply-templates select="." mode="Reject" />

That is an awful solution. It is brittle and not extensible code.

Please, please show me a better way than using xsl:choose.


Current Thread