Curtis
Curtis

Reputation: 103388

Conditionally nest elements in XSLT

I have an XSLT template which conditionally needs nesting in an anchor element.

For example:

<xsl:choose>
   <xsl:when test="$foo = 1">
     <a href="/bar">
       <xsl:call-template name="Baz">
          <xsl:with-param name="ParamA" select="$A" />
          <xsl:with-param name="ParamB" select="$B" />
          <xsl:with-param name="ParamC" select="$C" />
          <xsl:with-param name="ParamD" select="$D" />
          <xsl:with-param name="ParamE" select="$E" />
          <xsl:with-param name="ParamF" select="$F" />
       </xsl:call-template>
     </a>
   </xsl:when>
   <xsl:otherwise>
      <xsl:call-template name="Baz">
          <xsl:with-param name="ParamA" select="$A" />
          <xsl:with-param name="ParamB" select="$B" />
          <xsl:with-param name="ParamC" select="$C" />
          <xsl:with-param name="ParamD" select="$D" />
          <xsl:with-param name="ParamE" select="$E" />
          <xsl:with-param name="ParamF" select="$F" />
       </xsl:call-template>
   </xsl:otherwise>
</xsl:choose>

As you can see, theres a lot of duplicate code here around the call-template and the parameters passed.

Is there a tidier, DRYer, way of conditionally nesting elements?

Upvotes: 2

Views: 70

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

You could call the template in a variable, and place the result of the variable inside the choose.

Upvotes: 2

Related Questions