[xsl] Avoiding multiple "apply-templates" by creating one variable for the clauses. Is it possible?

Subject: [xsl] Avoiding multiple "apply-templates" by creating one variable for the clauses. Is it possible?
From: Kate Busch-Petersen <kate.busch-petersen@xxxxxxxxxxxxx>
Date: Thu, 20 Aug 2009 10:44:45 +0100
Hello,

I'm currently working on creating a blog in asp.net, using XML and XSLT 2.0.

In the past (working with XSLT 1.0) I've used multiple "apply-templates" to
handle all the possible param combinations, but I am looking for a more
manageable solution.

Current method:

    <xsl:choose>
        <xsl:when test="$AuthorId">
          <xsl:apply-templates select="//blog[author_id = $AuthorId]"/>
        </xsl:when>
        <xsl:when test="$CategoryId">
          <xsl:apply-templates select="//blog[category_id = $CategoryId]"/>
        </xsl:when>
        <xsl:when test="$AuthorId and $CategoryId">
          <xsl:apply-templates select="//blog[author_id = $AuthorId and
category_id = $CategoryId]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="//blog"/>
        </xsl:otherwise>
      </xsl:choose>

Obviously it's not ideal, and that's with just two params, in theory there
could be many more.

I was playing around with feeding the values to the "apply-template" using a
variable. Something along the lines of:

<xsl:variable name="SelectVar">
    1=1 <!-- always true -->
    <xsl:if test="$AuthorId">
      and author_id = <xsl:value-of select="$AuthorId"/>
    </xsl:if>
    <xsl:if test="$CategoryId">
      and category_id = <xsl:value-of select="$CategoryId"/>
    </xsl:if>
  </xsl:variable>

and then:

<xsl:apply-templates select="//blog[$SelectVar]"/>

Of course, this doesn't work.

Does anyone have any ideas for a more workable solution?

Many thanks,
Kate

Current Thread